Classes and Objects
C++ is an object oriented language.
An object oriented language works on the principle of objects - which are containers of data and functions which modify this data.
Objects are created with the help of classes.
Let us see what are these classes first.
Classes
An object has a state and a behavior similar to real life object.
State is data of the object. Behavior is what it can do.
e.g. if we consider a pencil - it has state - what is its color, length, diameter etc. It has behavior - it can write, it can get sharpened.
A class defines what are these state and behavior of an object.
A class is a template from which objects are created.
A class defines what data members and functions an object must have.
In programming paradigm, class is similar to data type and object is like a variable of this data type.
Object is one instance of a class.
Defining a class
A class is defined using the keyword class followed by its name and followed by class members. The entire class is enclosed within braces and is terminated with a semi colon.
Here we are having a class called dog. it has 3 data members - color, age and name. It also has a function members - bark(), eat() and run().
This class definition is a statement and like any other statement in C++, it must be terminated with a semicolon - ;
Creating objects
Let us see how to define few objects of this class.
We rewrote the previous definition of the class by adding implementations of all 3 methods.
Then we created an object of class dog with name tommy. Then we call three methods on this objects with the help of dot operator.
The output of the program is
$./a.out
Woof, woof
it eats
it runs
Dot operator
To access the members of an object we use dot operator(.) . It is similar to dot operator used with a structure in C. In the above example, tommy.bark() is used to call bark() function with tommy object.
Access specifiers - public, private and protected
In the previous program, the commented line tommy.age=3, is wrong. If we un-comment that line, the program throws a syntax error. Why?
Each member of a class has an access specifier associated with it - which tells who can access it. It specifies which members of the class are part of the interface of the class and so is accessible to users of the class (public) and which members are for internal use of the class, so are not accessible outside(private).
C++ has three access specifiers - public, private and protected.
public members of a class are accessible from the users of the class. They are visible everywhere. Most of the functions of a class are made public.
private members of a class are internal to the class. They can be accessed only from the member functions of the class. They are hidden from the users of the class.
The advantage of this data hiding is that programmer can change the implementation of a class without affecting the users.
Default access specifier for a class is private. So if you don't specify any access specifier, its members will be private.
protected members of a class are visible to the member functions and its sub-classes. We will see more about protected members later when we discuss inheritance.
In the previous class definition, age, color and name are all private members of dog class. But the methods bark(), eat() and run() are public as they are preceded by public specifier.
There can be multiple access blocks in a class. Once an access specifier is mentioned, all subsequent members belong to that access mode, until the next access specifier.
Let us look at another example.
In the class book defined above,
-
num and name are private as they don't have access specifier
-
author and price are public - they are written after public keyword
-
year is a protected member as it is preceded by protected keyword
-
lend_book() and return_book() are both public
The common practice in C++ is to
make data members of a class as private and methods of a class as public.
This will help users to access the interface of the class without worrying about implementation.
But there is a dilemma though. If data members are private, how can the user access them?
Data members of a class are accessed using methods called getters and setters and they are usually initialized with the help of special methods called constructors.
Difference between class and struct
C++ also has structures like C. But unlike structures in C, C++ structures can have function members also.
If that is the case, what is the difference between a class and a structure in C++?
The difference between structures and class is that struct members are public by default and class members are private by default.
But you can use access specifiers in structures too to change this default behavior.
Let us look at an example.
In the example above, we have defined a structure circle. It has members radius, x and y. It also has a member function - dispay_area().
We are changing radius of c1 object outside of structure - and we don't get any syntax error. Because radius and all other members of the class are public here.
We conclude this discussion with one last fact.
The implementations of methods are often defined outside of class body using scope resolution operator in .cpp file where as class body with member declarations are written in .h file. We will see that in depth in the next page.
Pointers to Objects
You can define pointers to objects and assign them to addresses of existing objects. Or you can create an object dynamically, using new operator.
In the line A *ptr2 = new A; an object is created, its constructor is called and address of object is stored in pointer ptr2.
Such dynamically created objects must be explicitly released using delete operator. delete operator releases memory and calls destructor for the object. delete operator is similar to free() function in C.
delete ptr2;
As in the case of structures in C, members of objects pointed by a pointer are accessed using arrow operator (->).
Here ptr is a pointer to the circle class and is dynamically allocated to a new circle object using new operator. Different functions - set_center(), set_radius() and print_area() are called using arrow operator. And finally the object is deleted using delete operator and memory is released.
Exercises :
1) Create a class book with name, author and price as data members. Create two functions print_book() and set_values() which will set the fields and display the information respectively.
2) Write a main function with 3 book objects and set values for all three.
3) What is default access specifier in a class? How do you make the members of a class accessible to everyone?
4) What is a dot operator? How is it different from an arrow operator?