Objects and Classes Part III CIS 61 Review

Objects and Classes Part III CIS 61

Review of What We Have Learned Ü Classes, syntax Ü Data, Member Functions Ü Objects Ü Constructors Ü Destructors Ü Overloaded Constructors

Member Functions Defined Outside the Class Ü So far we have seen member functions that were defined inside the class definition. Ü This isn’t always the case. Our last example showed a member function, add_dist(), this is declared inside the class “void add_dist(Distance, Distance);

Member Functions Defined Outside the Class Ü This statement in the class tells the compiler that this function is a member of the class but that it will be defined outside the class declaration. void Distance: : add_dist(Distance d 2, Distance d 3) The declarator in this definition contains some unfamiliar syntax.

Member Functions Defined Outside the Class Ü The function name, add_dist(), is preceded by the class name, Distance, and a new symbol double colon “: : ”. Ü This symbol is called the scope resolution operator. Ü It is a way of specifying what class something is associated with. Ü In this situation, Distance: : add_dist() means “the add_dist() member function of the Distance class”.

Objects as Arguments Ü Now we can see how our example ENGLCON works. Ü Let’s go back again and look at “englcon. cpp”.

Objects as Arguments Ü Every call to a member function is associated with a particular object. Ü Using the member names alone the function has direct access to all members whether private or public of that object. Ü It also has indirect access using the object name and the member name, connected with the dot operator to other objects of the same class that are

The Default Copy Constructor Ü There are 2 ways to initialize objects. Ü A no-argument constructor can initialize data members to constant values, and a multi-argument constructor can initialize data members to values passed as arguments. Ü There is one more way to initialize an object. Ü You can initialize it with another object of the same type.

The Default Copy Constructor Ü You don’t need to create a special constructor for this, one is already built into all classes. Ü This is called the default copy constructor. Ü This is a one-argument constructor whose argument is an object of the same class as the constructor. Ü Let’s look at “ecopycon. cpp”.

Returning Objects from Functions Ü In our englcon example, we saw objects being passed as arguments to functions. Ü Now we will see an example of a function that returns an object. Ü Let’s look at a modified version of the englcon program, “englret. cpp”.

Arguments and Objects Ü In englcon. cpp, two distances were passed to add_dist() as arguments, and the results were stored in the object of which add_dist() was a member. Ü In englret. cpp, one distance, dist 2, is passed to add_dist() as an argument. Ü dist 3=dist 1. add_dist(dist 2);

Arguments and Objects Ü Later on we will see how to do this same thing a 3 rd way, using operator overloaded and the statement would look like: dist 3 = dist 1 + dist 2;

A Card-Game Example Ü Here is a variation on an old example, a card guessing game. Ü Now this program has been rewritten to include objects. Ü This program will create 3 cards with fixed values and switches them around, then the user guesses where a certain card is located at. Ü Let’s look at “cardobj. cpp”.

Structures and Classes Ü You can make a structure look and act almost the same as a class. Ü Structures group data but they can also include functions just like classes. Ü Proper C++ uses structures just for data and uses classes to group data and functions. Ü In a class by default, data and functions are private members.

Classes, Objects and Memory Ü It is easiest and most useful to think of objects as containing separate copies of that class’s data and member function. Ü However this is not entirely true. Ü Each object of the same class has its own data, however it shares in memory the same space for its member functions.

Classes, Objects and Memory Ü Each object can have its own data, and in memory not each objects data will be the same throughout the program. Ü With each objects member function however, the code for each member function is the same for each object of the same class. Ü So we can get away with saving computer memory this way.

Static Class Data Ü If a data item is in a class that is declared as static, only one such item is created for the entire class. Ü A static data item is useful when all objects of the same class share a common item of information. Ü A member variable defined as static has characteristics similar to a normal static variable: it is visible only within a class, but it has the lifetime of the entire program.

Uses of Static Class Data Ü Why would you want to use static member data? Ü Maybe you have a race car driving game and for this game you need to know how many other cars there are in the race. Ü Each car would be an object, and you would use a static data member to keep track of the race car count. Ü Note that each object of type car would have this same data member and in

An Example of Static Class Data Ü Let’s look at an example of this, “statdata. cpp”.

const and Classes Ü The keyword const, tells the compiler that this variable’s value will not change in the entire program. Ü We can use this to our benefit in other ways. Ü If you have a function that doesn’t change any data, then you can use the keyword const with it. Ü Let’s look at an example of this,

const and Classes Ü If there is a separate function declaration, const must be used in both declaration and definition. Ü Member functions that do nothing but acquire data from an object are candidates for being made const, because they don’t need to modify any data. Ü Making a member function const helps because if you try to alter any values in a function that is const, the compiler will issue warnings telling you that you are trying to

const and Classes Ü Up to this point we have ignored const variables to avoid confusion. Ü Next we will look at our classic example of distances. Ü This time we will use the keyword const for a more complete program. Ü “engconst. cpp”.

const Objects Ü We have seen so far the keyword const applied to data members. Now lets look at const objects. Ü When an object is declared as const, you can’t modify it. Ü Let’s look at “constobj. cpp”.

What Does it All Mean? Ü Now that you’ve been introduced to classes and objects, you might wonder what benefit they really offer. Ü If you compare programs in this chapter to those in chapter 4, you could do the same things with a procedural approach as it is with objects.

What Does it All Mean? Ü One benefit of OOP is the close association between a real-world object and C++ objects. Ü Real objects have data and characteristics. C++ objects have data and uses member functions as characteristics. Ü In a procedural program, by contrast, the global variables and functions connected with a real world object are all over your code, they don’t form one easy to understand unit like in an object.

What Does it All Mean? Ü In some situations it is not so easy to see what parts of a real-life situation should be made into objects. Ü If your writing a program to play chess, what are the objects? The chessmen, the squares on the board, or maybe the entire board?

What Does it All Mean? Ü In small programs like those in our text book, you can often proceed by trial and error. Ü You can break the problem into objects in one way and write trial class definitions for these objects. Ü If the classes seem to work very well you can continue, if not you start over with your classes.

What Does it All Mean? Ü The more experience you have with OOP; the easier it will be to break a programming problem into classes. Ü Larger programs are too complex for this approach. Ü There are whole classes that you can take at a University in object-oriented design, OOD.

What Does it All Mean? Ü Some of the benefits of OOP is not apparent right now. Ü But remember that OOP was designed to help people create more and more complex programs. Ü Smaller programs like the ones we do in this class have less need for the organizational power that OOP provides.

What Does it All Mean? Ü The larger the program, the greater the benefit of OOP. Ü But even in smaller programs, once you start thinking in terms of OOP, this process is very helpful.
- Slides: 30