Understanding Classes and Objects in C++: Data Members, Member Functions, and Constructors, Study notes of Computer Programming

An introduction to classes and objects in c++ programming. It covers the definition of a class, the separation of interface from implementation, the structure of a class, and includes a sample program with constructors and default arguments. Learn about data members, member functions, and constructors in c++.

Typology: Study notes

2011/2012

Uploaded on 08/06/2012

anchal
anchal 🇮🇳

4.6

(9)

95 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
305
Lecture No. 26
Reading Material
Deitel & Deitel - C++ How to Program Chapter. 6
6.5, 6.7, 6.8, 6.10, 6.11, 6.14
Summary
x Classes and Objects
x Definition of a class
x Separation of Interface from the Implementation
x Structure of a class
x Sample program
x Constructor
x Default arguments with constructors
x Tips
Classes and Objects
In today’s lecture, we will try to learn about the concepts of ‘classes’ and ‘objects’.
However, we are not going to formally cover the object-oriented programming but
only the ways to manipulate the classes and objects.
We had talked about structures in our previous lectures. In structures, some data
variables are gathered, grouped and named as a single entity. Class and structure are
very closely related. In classes, we group some data variables and functions. These
functions normally manipulate these variables.
Before going ahead, it is better to understand what a class is:
“A class includes both data members as well as functions to manipulate that
data”
These functions are called ‘member functions’. We also call them methods. So a class
has data (the variables) and functions to manipulate that data. A class is a ‘user
defined’ data type. This way, we expand the language by creating a new data type.
When we create variables of a class, a special name is used for them i.e. Objects.
“Instances of a class are called objects”
With the definition of class, we have a new data type like int, char etc. Here int i;
means ‘i’ is an instance of data type int. When we take a variable of a class, it
becomes the instance of that class, called object.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Understanding Classes and Objects in C++: Data Members, Member Functions, and Constructors and more Study notes Computer Programming in PDF only on Docsity!

Lecture No. 26

Reading Material

Deitel & Deitel - C++ How to Program Chapter. 6

6.5, 6.7, 6.8, 6.10, 6.11, 6.

Summary

 Classes and Objects  Definition of a class  Separation of Interface from the Implementation  Structure of a class  Sample program  Constructor  Default arguments with constructors  Tips

Classes and Objects

In today’s lecture, we will try to learn about the concepts of ‘classes’ and ‘objects’. However, we are not going to formally cover the object-oriented programming but only the ways to manipulate the classes and objects.

We had talked about structures in our previous lectures. In structures, some data variables are gathered, grouped and named as a single entity. Class and structure are very closely related. In classes, we group some data variables and functions. These functions normally manipulate these variables. Before going ahead, it is better to understand what a class is:

“A class includes both data members as well as functions to manipulate that data”

These functions are called ‘member functions’. We also call them methods. So a class has data (the variables) and functions to manipulate that data. A class is a ‘user defined’ data type. This way, we expand the language by creating a new data type. When we create variables of a class, a special name is used for them i.e. Objects.

“Instances of a class are called objects”

With the definition of class, we have a new data type like int, char etc. Here int i; means ‘i’ is an instance of data type int. When we take a variable of a class, it becomes the instance of that class, called object.

docsity.com

Definition of a class Let’s have a look on the structure of a class. It is very similar to the struct keyword. Keyword class is used and the braces enclose the definition of the class i.e. class name_of_class{

// definition of class }

The new data type i.e. classes helps us to have grouped data members and member functions to manipulate the data. Consider a structure of Date having data members i.e. year, month and day. Now we can declare a variable of structure Date and use dot operator to access its members i.e.

Date myDate; myDate.month=3;

We have to use the name of the object, a dot operator and the data member of structure to be accessed. The data members are of normal data types like int, float, char etc. Other data types can also be used.

Let’s consider an example of Date Class shown in the following statement.

class Date{

int Day; int month; int year; };

Now we will take its object in the fashion given below:

Date myDate;

Separation of Interface from the Implementation To access the data members of the class, we will again use dot operator. Before going ahead, we will see what is the difference between struct and class. It is the visibility of the data members that differentiates between struct and class. What does the word ‘visibility’ mean? Consider an example of payroll system. We have stored the tax rate i.e. 5% in a variable i of type int. Later, we used the same i in a loop and changed the value of tax rate unintentionally. Now the calculation of the pay in the end will not provide the correct results. To avoid this problem, we can tag the tax rate variable as int tax_rate ;. But this variable again is visible in the whole program and anyone can change its value. The data is open and visible to every part of the program, creating a big problem. In normal programming, we will like to see the data encapsulated. It means that data is hidden somewhere. However, it can be used. Let’s consider a real world problem to understand it. Most of us have wrist-watches. To have accuracy, it is necessary to adjust the time. How can we do that? We can change the time by using the button that is provided on one side of the watch. This is a kind of encapsulation. We can see the

docsity.com

Normally, the data in a class is kept private. If we make the data public, it is same as structure and anyone can access this data. On the other hand, the functions which we have written to manipulate this data, are kept as public. These methods can be called from outside the class i.e. from the main program. These are the member functions of the class. The difference between these and the ordinary functions is that they are part of class. Moreover, they can see the private data members of the class and also manipulate them.

We have made the data members private in the Date class. In the program, we take an object of Date class as Data myDate ;. myDate is a variable of type Date. Now if we say myDate.month = 3; this statement will be illegal as the month is a private data member of the Date class. Now try to understand this concept. You can think class as a box having different things in it. How can we touch inside the box? We have a window and can see only those things that are visible through this window. Those things which we cannot see from the window, can not be accessed from outside. Day, month and year are somewhere inside the box and are not visible through the window. Now we want to assign some values to these data members. For this purpose, we will define a member function in the class in the public section. Being present in public section, it will be visible through the window. As this is the member function, it can see and manipulate the private data of the class. Now it’s a two-step process. We can see the public functions and public functions can view the private data members of the class. We will write a function to set the value to the month. We cannot write it as myDate.month = 10 ;. So our function prototype will be as:

void setMonth(int month)

and we may call this function as:

myDate.setMonth(10);

Now the function setMonth will assign the value 10 to month data member of the object myDate. The same thing will be applicable if we want to print the date. We can write a public function print and can access it as:

myDate.print();

The function print can see the private data members. So it will format the date and print it. In structures, the data members are public by default. It means that these are visible to all and anyone can change them. Is there any disadvantage of this? Think about the date. What may be the valid values of the day? Can we have a day less than zero or greater than 32. So the minimum and maximum values of the day are 1 and 31 respectively. Similarly, in case of month, the minimum and maximum values may be 1 and 12. We can assign different values to year like 1900, 2002 etc. If we are using Date structure instead of a class, we can write in the program as myDate.month=13; and the month will be set to 13. So the date will become invalid. We may want that other programmers also use this structure. But other programmers may put invalid values to the data-member as these are publicly accessible. Similarly in structures, everything is visible i.e. what are the names of the data members. How are these manipulated?

docsity.com

Now we want that only those things should be visible which we want to show and those things which we want to hide should not be visible We can get this by using the private and public in the classes. Public becomes the interface of the class, what we want to show to others. With the use of public interface, the objects can be manipulated. Private becomes the inside of the class i.e. the data members, the implementation. We don’t want to show the implementation of our classes to others. This is the concept of separation of interface from implementation. It is a crucially important concept in modern programming. We have separated the interface from the implementation. As long as the interface remains the same, the implementation can be changed. Let’s think about it in real world. The example from the automobiles sector can help us understand further. The production of cars in the world started in the late 18 th^ century and early 19th^ century. Let’s compare these early or prototype cars with today’s modern ones. There is a big difference between the old and new cars. Technology has changed. Now what is still common in both the types. Steering, clutch, brakes and accelerator pads are still the basic components of a car. So the interface is same. The internal functionality can be changed. To turn the car, old cars used rod mechanisms and modern cars have the microprocessor to do this job. Our physical action is same in both the cases. The interface i.e. steering is same and also the effect that wheels have turned to right is the same too. The internal implementation has completely changed. The old combustion engine cannot be compared with the state-of-the technology based modern engines. But the interface is the same i.e. we turn the key to start an engine. This concept of separation of implementation from interface comes into our programming. We have written a program today to calculate the orbital time of moon around the earth. In today’s physics, we have formula to calculate this. We have defined the interface calculateOrbitalTime(). This is a function that will calculate the orbital time of moon around earth. This formula may prove wrong after some time. Now what can we do? Despite the change in the implementation, interface remains the same i.e. the name of the function is same. Now when the program will use this function, it gets the correct result as we have implemented the new formula inside the function. Moreover, the main program does not need to be changed at all. Being a very neat concept, it can be used while dealing with objects and classes.

Sample program Let’s see the example of Date class in detail.

class Date { public: void display(); Date(int, int, int); private: int day, month, year; };

Date is the name of new user defined data type. After the braces, we have written the keyword public. In this section, we will define the interface of the class. We have declared a function display() which will print the date on the screen. Another function Date(int day, int month, int year) is declared. The name of this function is same as the name of the class, having no return type. This function is called constructor. Then we

docsity.com

void Date::setDay(int i) { day = i; }

void Date::setMonth(int i) { month = i; }

void Date::setYear(int i) { year = i; }

The question arises, which objects data members are being set. In the setDay function, we are assigning value to the day. But this day belongs to which object. The answer is, we have just defined the function, it is not called yet. The functions are called by the objects, not by the class. When we say Date myDate ; it means that we have an object of type Date. Now we can say myDate.setDay(10). The value of day of myDate object will be set to 10. When we create objects, these will reserve space in memory. Suppose, the objects are date1, date2, date3. These will be created at different memory locations having there own data members. When we call a member function with the object name, this function will manipulate the data of this object. Let’s consider the following code snippet to understand it.

Date date1, date2, date3; // Manipulating date1 object date1.setDay(10); date1.setMonth(12); date1.setYear(2002); date1.display();

// Manipulating date2 object date2.setDay(15); date2.setMonth(1); date2.setYear(2003); date2.display();

We have declared three objects of type Date. All these objects have data members day, month and year. When we call a function, that is defined in class, with some object name, it uses the data of that object which is calling the function. Suppose, when we write date1.setMonth(12); it will manipulate the data of object date. Similarly when we say date2.display() , the function is defined inside the class. However, it will use the data of date2 object. Remember that we will always call these member functions by referring to some specific object. We can call these functions with date1, date2 or date3 respectively. We will never call these functions referring to a class that is we cannot say Date.display(); It is illegal. The functions of getting data from objects and setting data of objects are standard. So we normally use

docsity.com

the word ‘set’ for setting the data and ‘get’ for getting the data. It is a matter of style. You can call it whatever you want. But it will be a bad idea to name a function print() and it is setting the value of month. It will work but in a very confused manner. If we want to write a function to set month, the logical choice of the function name is setMonth(int). Similarly, setDay(int) and setYear(int) will be used to set the day and year respectively. If we want to get the values of these data members, the logical choice will be getDay(), getMonth() and getYear(). The names are self-explanatory. These functions are defined as member functions of the class. They are put in the public section of the class and constitute the public interface of the class. These will be visible from outside the class. Normally they manipulate the data that is hidden inside the class i.e. in the private section of the class. No need to show the working of the functions only its name, argument and the return type is told to the user. User of the class is our program.

Here is the complete code of the Date class. /* A sample program with the Date class. Set methods are given to set the day, month and year.The date is also diplayed on the screen using member function. */

#include <iostream.h>

// defining the Date class class Date{ // interface of the class public: void display(); // to display the date on the screen void setDay(int i); // setting the day void setMonth(int i); // setting the month void setYear(int i); // setting the year

// hidden part of the class private: int day, month, year; };

// The display function of the class date void Date::display() { cout << "The date is " << day << "-" << month << "-" << year << endl; } // setting the value of the day void Date::setDay(int i) { day = i; }

// setting the value of the month void Date::setMonth(int i) { month = i;

docsity.com

Date(int, int, int);

This is the prototype of the constructor that is defined in the class. The definition of constructor is same as we used with the member functions.

Date::Date(int theDay, int theMonth, int theYear) { day = theDay; month = theMonth; year = theYear; }

How can we call this constructor? We know that constructor is automatically called when an object is created. To use this constructor, we will take an object as:

Date myDate(1, 1 , 2003);

Here two things have taken place. 1) An object is created 2) The data members are initialized. This is happening in the memory at run time. Nothing will happen at compile time. The constructor will be called after the object creation and before the control given back to the program. Here the value of day of the myDate object is 1, the value of month is 1 and the value of year is 2003. It has created and initialized an object. Now if we call the display() function. These values will be displayed. Constructor is used to initialized an object and put it into a consistent and valid state.

Default arguments with constructors We can also use the default arguments with the constructors. In the case of Date, normally the days and months are changing and the year remains same for one year. So we can give the default value to year.

Date::Date(int theDay, int theMonth, int theYear = 2002) { // The body of the constructor }

Now we have different ways of creating objects of class Date.

Date myDate;

In this case, the default constructor will be called while the data members remain un- initialized.

Date myDate(1, 1, 2000);

The constructor will be called and initialized. The day will be 1, month will be 1 and the year will be 2000.

Date myDate(1, 1);

docsity.com

The constructor will be called and initialized. The day will be 1, month will be 1 and the year will be initialized to the default value i.e. 2002.

There are some complications. Constructor is itself a function of C++ and can be overloaded. We can have many constructors. Suppose, we are asked to write the date i.e. 1, 1, 2000. Some of us may write it as 1, 1, 2000. Some will write it as 1/1/2000. A considerable number may write as 1-1-2000. One can write date as 1 Jan. 2000. There may have many formats of dates. It will be nice if we can initialize the object using any of these formats. So we may have a constructor which takes a character string. The date format is ’01-Jan-2003’. So the constructor should parse the string. The string before the hyphen is day (i.e. 01) convert it into an integer and assign it to day. Again get the strings before the 2nd^ hyphen (i.e. Jan), check which month is it (i.e. 1) and assign it to month. Rest of the string is year so convert it into integer and assign it to year. We are doing a lot of horizontal integration here. The good thing is that the rules of simple functions overloading applies to constructors also. The rules of default arguments also apply while we are using default arguments with constructors. The idea is to make the class as friendly as possible for the users. We have two constructors. Of these, one takes three ints and the other takes the date as a character string. We may want to add more constructors. But we don’t want to add too many constructors in the class as there is a limit of everything. Within limits and the reasons, provision of two to three alternatives to the users of the class for object creation is nice. May be the program that is using our class, is applying months as character strings. We should provide a constructor that deals with this. We will further explain this subject in the coming lectures. A constructor is a special kind of function having same name as that of a class. It has no return type. Declare it without return type. Constructor can take arguments. The default constructor takes no argument. Here is the code of the Date class using the different constructors.

/* A sample program with the Date class. Use of constructors is shown here. */

#include <iostream.h> //#include <stdlib.h>

// defining the Date class class Date{ // interface of the class public: void display(); // to display the date on the screen void setDay(int i); // setting the day void setMonth(int i); // setting the month void setYear(int i); // setting the year int getDay(); // getting the value of day int getMonth(); // getting the value of month int getYear(); // getting the value of year

// Constructors of the class Date(); Date(int, int, int);

docsity.com

return day; }

// getting the value of the month int Date::getMonth() { return month; }

// getting the value of the year int Date::getYear() { return year; }

// Main program. We will take three date objects using constructors, and display the date. int main() { Date date1, date2(1, 1, 2000), date3(10,12); // taking objects of Date class

// displaying the dates on the screen date1.display(); date2.display(); date3.display();

}

The output of the program is: The date is 1-1- The date is 1-1- The date is 10-12-

Summary A class is a user defined data type. It has data members and member functions. Normally member functions are called methods. Data members are generally kept as private. The member functions, used to manipulate the data members, are kept public so that these are visible from outside the class. The public part of the class is known as the interface of the class. It may contain data members and functions but normally we put functions as public. The member functions can manipulate the data members (public and private) of the class. Non-member functions can not see or access the private part of the class. We try to separate the implementation of the class from its interface.

Tips  Explicitly write keyword private in the class definition

docsity.com

 Separate the interface and implementation  The default constructor has no arguments  Constructor has the same name as of class  The data members of the class are initialized at runtime  Initializing the data members in the definition of the class is a syntax error

docsity.com