Understanding Classes and Objects in C++: Data Members, Member Functions, and Constructors, Exercises of Programming for Engineers

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 the use of constructors with default arguments. A sample program and tips for working with classes and objects.

Typology: Exercises

2011/2012

Uploaded on 07/25/2012

anasuya
anasuya 🇮🇳

4

(9)

85 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
______________________________________________________________________
Page 1 of 13
Reading Material
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.
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.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Understanding Classes and Objects in C++: Data Members, Member Functions, and Constructors and more Exercises Programming for Engineers in PDF only on Docsity!

Page 1 of 13

Reading Material

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.

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.

Page 2 of 13

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 hands of the watch but cannot touch them. To change their position we used the button. Whenever we talk about the class, we have to think of this concept that data is available somewhere. We don‟t need to know about the exact structure i.e. what is inside the watch. All we know is that its internal structure is defined somewhere that cannot be seen or touched. We can only see its interface. If we need to adjust the time, a button may be used. It is a nice separation of implementation and interface. Classes allow us to do that.

Structure of a class Let‟s have a look inside a class. Consider the example of class Date. Can we set the values of the data members of the object „myDate‟ i.e. day, month or year. We cannot say like myDate.month = 11 ;. Try to do this. The compiler will give error and stop compiling the program. It will not recognize the variable „month‟. In other words, it cannot see „month‟. The default visibility for the data members of the class is called „private‟. These can only be used within the class and are not visible outside.

“The default visibility of all the data members and member function of a class is

Page 4 of 13

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?

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.

Page 5 of 13

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 write the keyword private and define the implementation of the class. Here we have three variables i.e. day, month and year of type int. In the end closing braces and the semi-colon. This is the definition of user defined data type i.e. class. It will not occupy any memory space as it has no data currently. It is the same as we write in case of ‘int’. It does not occupy any memory but when we say int I, the memory is reserved for i. The class is a „user defined data‟ type. Now in our program, when we write Date myDate ; an instance of the class is created i.e. an object. Object reserves space in the memory. Object will have these data members. What about the „functions‟? For a moment, we can say that functions are also in the memory.

We want to use this class in our program and display the date using the display() function. We have written the prototype of the display() function in the class without defining the display() function yet. A special way is used to define these functions. We will write the name of the class, followed by two colons and the name of the function. The rest is same as we used to do with ordinary functions. , Date::display() { // the definition of the function cout << “The date is “ << day << “-“ << month << “-“ << year << endl; }

You might have noted the difference in the first line. The double colon is called scope resolution operator. It resolves the scope and tells that this function belongs to whom. In this case, the ( Date::display()) ) tells that the display() function belongs to the Date class. So the scope resolution is required. In a way, consider it as function is defined inside the class. If you have private function, even then the definition mechanism is same. We will define the function outside of the class. Even then it will not be visible as its visibility is private. The way to define the member functions is, class name, double colon, name of the function including arguments and then the body of the function. Can we define the function inside the class? Yes we can. When we write the function inside the class, the compiler tries to treat that function as inline function. As a good programming practice, we define the functions outside of the class. So to make sure that the function belongs to the class, the scope resolution operator is used.

Page 7 of 13

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 date1. 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 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

Page 8 of 13

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

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

// setting the value of the year void Date::setYear(int i) { year = i; } // Main program. We will take two date objects, set day, month, year and display the date. int main() { Date date1,date2; // taking objects of Date class

// setting the values and displaying date1.setDay(1); date1.setMonth(1); date1.setYear(2000); date1.display();

// setting the values and displaying date1.setDay(10); date1.setMonth(12); date1.setYear(2002); date1.display(); }

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

Constructors

We have written a function named Date(int, int, int) in our class. This is in the public section of our class. It has no return type, having the name as that of class. Such functions are called constructors. When we create an object by writing Date myDate; A function is invisibly called which does something with this object. This function is constructor. If we do not write a constructor, C++ writes a default constructor for us. By and large, we want that the object should be created in a certain state. When our object myDate is created its data members-day, month and year have some value. We can initialize these data members with zero or with some specific date. How can we do that? Native data types can be initialized as:

Page 10 of 13

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

Date myDate(1, 1);

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

Page 11 of 13

int getYear(); // getting the value of year

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

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

// defining the constructor // default constructor. setting the date to a default date

Date::Date() { day = 1; month = 1; year = 1900; }

// Constructors with default arguments

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

// The display function of the class date void Date::display() { cout << "The date is " << getDay() << "-" << getMonth() << "-" << getYear() << 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; }

Page 13 of 13

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  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