Chapter 3 Classes and Objects
Chapter 3 Classes and Objects
Chapter 3 Classes and Objects
Class:
A class is a data type defined by the user describing the data it represents and the
function to be used to manipulate the data. It seems as a plan/template/blue
print.
Object:
Objects are instances of class, which holds the data variables declared in class and
the member functions work on these class objects. We can define many objects of
the same class. Class and their objects can be defined according to the problem at
hand. e.g. Animal is a class and cat, dog, lion, tiger objects of class animal.
Creating classes:
A class is defined in C++ using keyword class followed by the name of class. The body of class is
defined inside the curly brackets and terminated by a semicolon at the end.
Creating Classes (Another answer; for Long Question)
A class is a way to bind the data and its associated functions together. It allows the data (and
functions) to be hidden, if necessary, from external use. When defining a class, we are creating
a new abstract data type that can be treated like any other built-in data type. The general
form of a class declaration is:
class class_name
{
private:
variable declaration;
function declarations;
protected:
variable declaration;
function declarations;
public:
variable declarations;
function declaration;
};
The class declaration is similar to a struct declaration. The keyword class specifies, that follows
is an abstract data of type class_name. The body of a class is enclosed within braces and
terminated by a semicolon. The class body contains the declaration of variables and functions.
These functions and variables are collectively called class members.
The keywords private,protected and public are known as visibility labels or access specifier.
Example of Creating class:
class student
{
int roll_number;
float marks;
public:
void insertdata(int a, float b);
void displaydata(void);
};
Example:
class Cube
{
public:
int side;
int getVolume()
{
return side*side*side; //returns volume of cube
}
};
Example:
class Cube
{
public:
int side;
int getVolume();
}
1) A program can have several classes and they can have member functions with the same
name, Ambiguity is resolved using the scope resolution operator (::)
2) Private members of a class can be accessed by all the members of the class, whereas non-
member functions are not allowed to access.
3) Member functions of the same class can access all other members of their own class
without the use of dot operator.
4) Member functions defined as public act as an interface between the service provider and
the service seeker.
5) Member functions can take default arguments as well.
The properties of a static member variable are similar to that of a C static variable. A static
member variable has certain special characteristics. These are:
• It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
• Only one copy of that member is created for the entire class and is shared by all the objects
of that class, no matter how many objects are created.
• It is visible only within the class, but its lifetime is the entire program.
Static variables are normally used to maintain values common to the entire class. For example,
a static data member can be used as a counter that records the occurrences of all the objects.
The type and scope of each static member variable must be defined outside the class
definition. This is necessary because the static data members are stored separately rather than
as a part of an object. Since they are associated with the class itself rather than with any class
object, they are also known as class variables. The static variable a is initialized to zero when
the objects are created.
Example 2:
#include<iostream>
using namespace std;
class my
{
static int count; //declaration of one data item for all objects
public:
my ( )
{
count ++;
}
int getcount ( )
{
return count;
}
};
int my::count = 0; // definition of count which alloates memory for count, and initializes it
int main ( )
{
my f1,f2,f3;
cout<<"count is "<<f1.getcount ( )<<endl;
cout<<"count is "<<f2.getcount ( )<<endl;
cout<<"count is "<<f3.getcount ( )<<endl;
return 0;
}
Output:
count is 3
count is 3
count is 3
Friend Function
A friend function is a function that is not a member of a class but has access to the
class's private and protected members.
The concepts of data hiding and encapsulation dictates that private and protected
members of a class cannot be accessed from outside the class. This is non-member
function of a class cannot access the non-public members (data members and
member functions) of a class. However, we can achieve this by using friend
functions. The friend functions allow operations between two different classes.
Generally the use of friend functions is out of an object-oriented proframming
methodology because it violates the concept of data hiding. So whenever possible
it is better to use members of the same class. To make an outside function friendly
to a class, we simply declare the function as a friend of the class.
Some special characteristics of friend functions are:
1. Since, it is not in the scope of the class to which it has been declared as a friend,
it cannot be called using the object of the class.
2. It can be invoked like a normal function without the help of any object.
3. Unlike member functions, it cannot access the member names directly and has
to use an object name and dot membership operator with each member name.
4. It can be declared either in the public or the private part of a class without
affecting its meaning.
5. It has the objects as arguments.
Friend function also acts as bridging between two classes. For example, if
we want a function to take objects of two classes as arguments and
operate on their private members, we can inherit the two classes from
the same base class and put the function in the base class.
Example
#include<iostream>
using namespace std;
class A
{
int a;
int b;
friend void sum(A ob);
public: void setvalue(int x, int y)
{
a=x;
b=y;
}
};
void sum(A ob)
{
cout<<ob.a+ob.b;
}
main()
{
A obj;
obj.setvalue(5,3);
sum(obj);
}
Output
8
Protected access modifier is similar to that of private access modifiers, the difference is that
the class member declared as Protected are inaccessible outside the class but they can be
accessed by any subclass (derived class) of that class.
(For more and the example look at the ACEM note)
6. We know that a private member of a base class is not inheritable. Is it any way
possible for the objects of a derived class to access the private members of the
base class? If yes, how?
[Remember, the base class cannot be modified.]
We know that member of one class can not access the private data of other
class. Some time it is needed that we want to make available private data of
one class to other class. In such case we need to make one class to a friend of
other class.
A base class's private members are never accessible directly from a derived
class, but can be accessed through calls to the public and protected members
of the base class.