Objects and Classes Chapter 6 CIS 61 Objects
Objects and Classes Chapter 6 CIS 61
Objects and Classes ¨ So far we have studied structures which provide a way to group data elements. ¨ So far we have studied functions, which organize program actions into named entities. ¨ Now we will put these idea’s together to create classes.
Objects and Classes ¨ Let’s first see a program example, then explain the differences and details. ¨ Let’s look at “smallobj. cpp”.
Classes and Objects ¨ Remember that an object has the same relationship to a class that a variable has to a data type. ¨ An object is said to be an instance of a class, in the same way that a Dodge Viper is an instance of a vehicle.
Classes and Objects ¨ In our last example the class “smallobj” was defined in the first part of the program. ¨ Later in main(), two objects were defined, s 1 and s 2, those are instances of the class smallobj. Think of a class as a blueprint, and the object is the actual, tangible thing, or object.
Defining the Class ¨ The definition of a class starts with the keyword class, followed by the class name. ¨ Just like a structure, the body of a class is delimited by braces and terminated by a semicolon. class smallobj //define a class { private: int somedata; //class data public: void setdata(int d) //member function to set data { somedata = d; } void showdata() //member function to display data { cout << "Data is " << somedata << endl; } };
Private and Public Keywords ¨ A key feature in object-oriented languages is data hiding. ¨ This term data hiding, means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside of the class. ¨ To hide data, you put it in a class and make it private.
Private and Public Keywords ¨ Private data or functions can only be accessed from within a class. ¨ Public data or functions, on the other hand, are accessible from outside the class.
Hidden from whom? ¨ Don’t confuse data hiding from security techniques in software. ¨ Data hiding means hiding data from parts of the program that don’t need to access it. ¨ More specifically, one class’s data is hidden from other classes. ¨ Data hiding is designed to protect wellintentioned programmers from honest mistakes.
Class Data ¨ The smallobj class in our example contains one data item: somedata, which is of type int. ¨ The data items within a class are called data members. ¨ There can be any number of data members in a class, just as there can be any number of data items in a structure.
Member Functions ¨ Member Functions are functions that are included within classes. ¨ In our last example we had 2 member functions, setdata() and showdata(). ¨ You can define the member functions in the class or more common is outside of your class which we will see later on.
Member Functions ¨ In our last example the member functions follow the keyword public. ¨ Since they are declared as public, they can be accessed from outside of the class.
Functions are Public, Data is Private ¨ Usually the data within a class is private and the functions are public. ¨ This is a result of the way classes are used. ¨ The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed outside of the class.
Functions are Public, Data is Private ¨ However there is no rule that says data must be private and functions public. ¨ In some situations, you may find you’ll need to use private functions and public data.
Using the Class ¨ Now that we have the class defined, let’s see how main() will take use of it. ¨ In our last example, the first statement in main() was “smallobj s 1, s 2; ” ¨ This statement defines two objects of class smallobj.
Using the Class ¨ Remember that the definition of the class does not create any objects. ¨ The definition only describes how they will look when they are created. ¨ Objects are what participate in program operations. ¨ Defining an object is similar to defining a variable of any data type, space is set aside for it in memory.
Calling Member Functions ¨ In our example, the next 2 statements in main() call the member function setdata(): ¨ s 1. setdata(1066); ¨ s 2. setdata(1779); ¨ These statements don’t look like normal function calls.
Calling Member Functions ¨ Why are the object names s 1 and s 2 connected to the functions names with a period? ¨ This strange syntax is used to call a member function that is associated with a specific object. ¨ Because setdata() is a member function of the smallobj class, it must always be called in connection with an object of this class.
Calling Member Functions ¨ So it doesn’t make sense to say : setdata(1066); ¨ This is because a member function is always called to act on a specific object. ¨ Member functions can only be accessed by an object of that class.
Calling Member Functions ¨ To use a member function, the dot operator connects the object name and the member function. ¨ The syntax is similar to the way we refer to structure members.
Calling Member Functions ¨ The first call to setdata(), “s 1. setdata(1066); executes the setdata() member function of the s 1 object. ¨ This function sets the variable somedata in object s 1 to the value 1066.
C++ Objects as Physical Objects ¨ In many programming situations, objects in programs represent physical objects. ¨ Our next example are widget parts in an inventory. ¨ Let’s look at “objpart. cpp”.
- Slides: 22