






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Sir Prachur Sharma gave this handout to study for basic and important concepts related Programming for Engineers course at Bhagwant University Rajasthan. It includes: Classes, Object, Constructor, Types, Utility, Functions, Destructors, Building, Blocks, Strings, Arrays, Variables, Process
Typology: Exercises
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Page 1 of 10
This lecture is a sequel of the previous discussion on 'Classes' and 'Objects'. The use of 'classes and objects' has changed our way of thinking. Instead of having function-oriented programs i.e. getting data and performing functions with it, we have now data that knows how to manipulate itself. This way, now the programming is object-oriented. It means that our programs revolve around data and the objects. Therefore, it would be nice to have some building blocks for programs so that these can be combined to write a program. We have so far talked about simple variables like integer, double and char, followed by strings of characters and arrays of different data types. But now, in the form of an object, we have a block which knows itself about contents and the behavior. The upcoming discussion will further explain it. We have used cout for displaying many things like integers, doubles and strings. Here integer did not know how it is going to display itself. However, cout knows how to display an integer on the screen. Now we want to see that an integer should know how to display itself. So it will be a different process. Now the question arises whether it will be good to see an integer knowing how to display itself? For this purpose, we will have to expand the scope of thinking. While engaged in the process of programming, we try to solve a real-world problem. The real world is not only of integers, floats, doubles and chars, but there are other things like cycles, cars, buildings, schools and people. We perceive all these things as objects. Each object has a behavior associated with it. Consider the example of a man who can talk, walk, sit and stand etc. Similarly, we can think of a vehicle that has many functions. These objects also have attributes. For example, a man has height, weight, color of eyes and hair and so on. These all are his attributes. His actions will be referred as functions or methods. This principle may be applicable to vehicles, aeroplanes and all other real-world things. An aeroplane has attributes like its height, width, number of seats and number of engines etc. These are attributes called data members. Its actions include takeoff, flying and landing. These actions are its functions or methods. In terms of language, the attributes and actions may be equated with nouns and verbs. The verbs are actions which are also called methods in the programming terminology. These methods should be included in the object in such a way that the object itself knows how to achieve this function or method. Now consider this all in terms of data. Is it always in terms of salary, payroll, amount and numbers? Actually, data comes in different varieties. Now a day, a computer is a multimedia equipment. We see that there are not only alphabets and digits displayed on the screen, but also pictures, images, windows, dialogue boxes, color and so many other things. These are numbers, letters and graphics. Other than this, we can see videos on our computer. It is just another type of media. Similarly, we find audio material, which can be played on the computer. Thus in the expanded terms
Page 2 of 10
of data, we come across numbers, pictures, audio and video while dealing with multimedia. Now we think about the concept that an integer should know how to display itself. With the enhanced scope of data, we can also have an audio, which knows how to play itself. The same applies to video.
Class A class is a way of defining a user-defined data type. In a class, one may find data members and functions that can manipulate that data. In the previous lectures, we have talked about the concept of data hiding i.e. encapsulation that means that the data of a class cannot be accessed from outside. However, it can be done through some defined functions (methods). These are the member functions of the class. To hide the data, we declare it private. If a data is private, it will be available only to member functions of the class. No other function outside the class (except friend functions) can access the private data. Normally in a class we divide the private part which is normally what we called implementation of the class, from the functions that manipulate that private data which is called the interface (which is the front end). The example of a room can help us understand private and public parts of a class. A class is a room having a curtain in its middle. The things behind the curtain (private) are visible to the residents (insiders) of the room. They know about every thing present in the room. When the door opens, the outsiders see only the things in front of the curtain. This is the public interface of the class while behind the curtain is the private interface. A function inside the class (i.e. a member function) can access and manipulate all things in the class. A function outside the class can only access and manipulate its public interface part. A constructor has to be in the public section of the class. There should also be a public interface so that it can be called from outside.
Constructor is a special function, called whenever we instantiate an object of a class. If we do not define a constructor function in a class, the C++ provides a default constructor. It is executed at the time of instantiating an object. To understand the basic function of constructor, we have to go back. While writing c++ Stroustrup noticed that the majority of programming problems, which we call bugs, occur due to the use of uninitialized data. That is, we declare variables and use them without providing them any value. For example, we declare an integer as int i ; And it is not initialized with a value like i= 0; or i = 5; And then somewhere in the program, we write, say, j = 2 * i ;. This is the usage of an uninitialized data variable. This technique has a demerit that despite having no syntax error, it may cause a logical error, which is difficult to find. Thus, initialization of data is a very critical activity. The constructors give us an opportunity to initialize the data members of an object in such a way that when our program gets an object, the data part of the object is in a known state. Being in a valid state, it can be used. The constructors are used to initialize the data members of an object. A class is a user defined data type it does not take space in the memory unless we create an object from it. The constructors create space for data members and
Page 4 of 10
body of the construct function cannot have any return statement. Otherwise, the compiler will give a syntax error.
Explanation of Constructors The main purpose of the constructor is to initialize the object in such a manner that it is in a known valid state. Consider the example of Date class again. In that example, there were three data members i.e. day, month and year of type int. What values will we give to these data variables by default if we create an object of Date? There may be any valid date. We can give a value 01 to day, month and 1901 to year or what ever we want. It will be a known state despite being meaningless. We can write a constructor of class Date which takes three arguments int day, int month and int year, and puts values in the data members of the object, being created. Now the question arises when does this happen? It happens when we instanciate an object of class Date by writing Date myDate; When this line executes in the program, some space for 'myDate' is reserved in the memory. This space contains the space for day, month and year variables. Then control goes to the constructor that assigns values to day, month and year. Being a member of the class, the constructor can write values to the data members of the class that is private..
In C++ language, we can provide default arguments to functions. As a function, the constructor can take default arguments. Suppose we have written a constructor of class date with the arguments by providing default values to its arguments. We can write a constructor as Date (int day=1, int month=1, int year=1); and create an object of class Date as Date myDate;
(constructor with no argument) in case of providing a fully qualified constructor (with default values for all arguments). Now suppose, we want to initialize the Date object properly by passing a character string to its constructor. Is it possible to write such a constructor? Yes, we can write such a constructor. This constructor will take date as a string, say, 01-jan-1999. And in the constructor, we can split up this string with 'string manipulation functions' and assign respective values to day, month and year.
Now we recapture the concept of constructors with special reference to their characteristics.
A constructor is a function which has the same name as the class. It has no return type, so it contains no return statement.
Page 5 of 10
Whenever an instance of a class comes into scope, the constructor is executed. The constructors can be overloaded. We can write as many constructors as we require. At one time, the compiler will call the correct version of the constructor".
The second issue, we usually come across while dealing with the concepts of 'classes and objects' is that a class has a data on one side (normally private part) and functions on the other (normally public part). The functions (methods) are normally written in public part of the class. Are there functions which are private to a class? Answer is yes. The functions of a class may be of two categories. One category contains the member functions which manipulate the data or extract the data and display it. Through these, we can set and get values to manipulate data. These are the functions which are in public interface of the class and manipulate the data in the object. But sometimes, we need such functions that is the requirement of these member functions. Suppose we write a setDate function. This function is given an argument and it does the same thing as done by the constructor. In other words, it sets a value of date. Now that function can be public so that it can be called from outside the class. Now we want that the member functions of the class can call this function. But it should not be called from outside. In this case, we put this function in private section of the class. These functions are called utility functions. These are a utility used by other methods of the class. However, they are not functions, supposed to be accessed from outside the class. So they are kept private.
The name of the destructor is the same as that of a class with a preceding tilde sign (~). The ~ and name of the class is written as a single word without any space between them. So the name of the destructor of class Date will be ~Date. The destructor can not be overloaded. This means that there will be only one destructor for a class. A destructor is automatically called when an object is destroyed. When does an object gets destroyed? When we create an object in a function, this is local to that function. When the function exits the life of the object also comes to end. It means that the object is also destroyed. What happens if we declare an object in the main program? When the main program ends, its objects also comes to end and the destructor will be called. The destructor is normally used for memory manipulation purposes. Suppose we have such a class that when we create an object of it then its constructor has allocated some memory. As we know that we have to free the allocated memory to ensure its utilization for some other program. The destructor is used normally for this purpose to make sure that any allocated memory is de-allocated and returned to free store (heap).
The destructors can be summarized as the following.
The destructors cannot be overloaded. The destructors take no arguments. The destructors don’t return a value. So they don’t have a return type and no return statement in the body.
Page 7 of 10
operate on the data of that object. Thus only the value of the month of date1 will be set to
A sample program with the Date class. Use of constructors and destructor is shown here. A message is displayed to show which one constructor is called */
#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
Page 8 of 10
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); Date(int, int, int); // Destructor of the class ~Date (); // 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; cout << "The default constructor is called" << endl; }
// Constructors with two arguments
Date::Date(int theDay, int theMonth) { day = theDay; month = theMonth; year = 2002; cout << "The constructor with two arguments is called" << endl ; } // Constructors with three arguments
Date::Date(int theDay, int theMonth, int theYear) { day = theDay; month = theMonth; year = theYear; cout << "The constructor with three arguments is called" << endl; }
Page 10 of 10
/* Main program. We will take three date objects using the three constructors (default, two arguments and three arguments and display the date. */ int main() { Date date1, date2(12,12), date3(25,12,2002); // taking objects of Date class
// displaying the dates on the screen date1.display(); date2.display(); date3.display(); }
Following is the output of the above program. The default constructor is called The constructor with two arguments is called The constructor with three arguments is called The date is 1-1- The date is 12-12- The date is 25-12- The object has destroyed The object has destroyed The object has destroyed