Classes and Objects
Classes and Objects
Class
Class in C++ is the building block that leads to Object-Oriented programming.
It is a user-defined data type, which holds its own data members and member functions, which
can be accessed and used by creating an instance of that class.
A C++ class is like a blueprint for an object. For Example: Consider the Class of Cars.
There may be many cars with different names and brands but all of them will share some common
properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the
class, and wheels, speed limits, and mileage are their properties.
A Class is a user-defined data type that has data members and member functions.
Data members are the data variables and member functions are the functions used to manipulate
these variables together, these data members and member functions define the properties and
behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage, etc, and member
functions can be applying brakes, increasing speed, etc.
Object
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
Defining Class and Declaring Objects
A class is defined in C++ using the keyword class followed by the name of the class. The body of
the class is defined inside the curly brackets and terminated by a semicolon at the end.
Declaring Objects
When a class is defined, only the specification for the object is defined; no memory or storage is
allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax
ClassName ObjectName;
Accessing data members and member functions: The data members and member functions of
the class can be accessed using the dot(‘.’) operator with the object. For example, if the name of
the object is obj and you want to access the member function with the name printName() then you
will have to write obj.printName().
Accessing Data Members
The public data members are also accessed in the same way given however the private data
members are not allowed to be accessed directly by the object. Accessing a data member depends
solely on the access control of that data member. This access control is given by Access modifiers
in C++. There are three access modifiers: public, private, and protected.
Access Modifiers :
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class
members, i.e., they set some restrictions on the class members so that they can’t be directly
accessed by the outside functions.
Public
Private
Protected
Note: If we do not specify any access modifiers for the members inside the class, then by default
the access modifier for the members will be Private.
Let us now look at each one of these access modifiers in detail:
1. Public: All the class members declared under the public specifier will be available to
everyone. The data members and member functions declared as public can be accessed by
other classes and functions too. The public members of a class can be accessed from anywhere
in the program using the direct member access operator (.) with the object of that class.
2. Private: The class members declared as private can be accessed only by the member functions
inside the class. They are not allowed to be accessed directly by any object or function outside
the class. Only the member functions or the friend functions are allowed to access the private
data members of the class.
3. Protected: The protected access modifier is similar to the private access modifier in the sense
that it can’t be accessed outside of its class unless with the help of a friend class. The difference
is that the class members declared as Protected can be accessed by any subclass (derived class)
of that class as well.
Note: This access through inheritance can alter the access modifier of the elements of base class
in derived class depending on the mode of Inheritance.
Program on Classes and Object
#include<iostream.h>
#include<conio.h>
class addition
{
private:
int a , b;
public:
void getdata()
{
cout<<“\n Enter two numbers: ”;
cin>>a>>b;
}
void putdata()
{
cout<<“Addition = ”<<a+b;
};
int main()
{
addition ad ;
ad.getdata();
ad.putdata();
getch();
return 0;
}
Inheritance
The capability of a class to derive properties and characteristics from another class is
called inheritance Inheritance is one of the most important features of Object Oriented
Programming in C++.
In C++, inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically. In such way, you can reuse,
extend or modify the attributes and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived
class and the class whose members are inherited is called base class. The
derived class is the specialized class for the base class.
Advantage of C++ Inheritance
Code reusability: Now you can reuse the members of your parent class. So,
there is no need to define the member again. So less code is required in the
class.
Types Of Inheritance
C++ supports five types of inheritance:
Derived Classes
A Derived class is defined as the class derived from the base class.
Visibility Modifiers
Where 'A' is the base class, and 'B' is the derived class.
Single inheritance
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int x;
public:
void input()
{
cout<<“\n Enter the number”;
cin >>x;
}
};
Class B : public A
{
int y;
public :
void getdata()
{
cout<<“\n Enter the no.”;
cin>>y;
}
void putdata()
{
cout<<“\n Addition = ”<<x+y;
}
};
int main()
{
B aa;
aa.getdata();
aa.putdata();
getch();
return 0;
}
Multiple inheritance
Multiple Inheritance is the concept of the Inheritance in C++ that allows a child class to inherit
properties or behaviour from multiple base classes.
Therefore, we can say it is the process that enables a derived class to acquire member functions,
properties, characteristics from more than one base class.
In the above diagram, there are two-parent classes: Base Class 1 and Base Class 2, whereas there
is only one Child Class.
The Child Class acquires all features from both Base class 1 and Base class 2. Therefore, we
termed the type of Inheritance as Multiple Inheritance.
Syntax:-
class A
{
// code of class A
}
class B
{
// code of class B
}
class C: public A, public B (access modifier class_name)
{
// code of the derived class
}
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void input()
{
cout<<"\n Enter the No.: ";
cin>>a;
}
};
class B
{
protected :
int b;
public:
void getdata()
{
cout<<"\n Enter the No. :";
cin>>b;
}
};
class c: public A, public B
{
public :
void addition()
{
cout<<"\n Addition = "<<a+b;
}
};
int main()
{
clrscr();
c ob;
ob.input();
ob.getdata();
ob.addition();
getch();
return 0;
}
Multilevel inheritance
Multilevel inheritance is a type of inheritance, where a class is derived from another derived class,
creating a chain of inheritance that allows it to pass down its properties and behaviors through
multiple levels of classes or inherit from its predecessor.
Syntax :-
class baseClass {
//Here's a base class members
};
class derivedClass1 : public baseClass {
// Members of derivedClass1
};
class derivedClass2 : public derivedClass1 {
// Members of derivedClass2
};
Here,
•baseClass is the top-level class from where other classes derive.
•derivedClass1 is the class that inherits from baseClass.
•derivedClass2 Inherits from derivedClass1, creating a multilevel structure.
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int roll;
public:
void getroll()
{
cout<<"\n Enter the Roll No. :";
cin>>roll;
}
void putroll()
{
cout<<"\n Roll No = "<<roll;
}
};
class B: public A
{
protected:
int sub1, sub2;
public :
void getmarks()
{
cout<<"\n Enter the marks of 2 subjects: ";
cin>>sub1>>sub2;
}
void putmarks()
{
cout<<"\n marks1= "<<sub1;
cout<<"\n marks 2= "<<sub2;
}
};
class c: public B
{
int sports;
public :
void getsports()
{
cout<<"\n Enter the sports marks: ";
cin>>sports;
}
void total()
{
putroll();
putmarks();
cout<<"\n Total Marks = "<<sub1+sub2+sports;
}
};
int main()
{
c ob;
ob.getroll();
ob.getmarks();
ob.getsports();
ob.total();
getch();
return 0;
}
Hierarchical inheritance
The concept of inheritance is very similar to the real world. Just like a son inherits the properties
(characteristics and behavior) of his father and father himself inherits the properties of the son's
grandfather.
In programming norms, inheritance occurs when one class inherits the properties of another
class(base).
Definition
As the name defines, it is the hierarchy of classes. There is a single base class and multiple
derived classes. Furthermore, the derived classes are also inherited by some other classes. Thus a
tree-like structure is formed of hierarchy.
Here class A is the base class. Class B and Class C are the derived classes of A.
Class D and Class E are derived classes of B. Class F and Class G are derived classes of C. Thus
forming the structure of hierarchical inheritance.
Where hierarchical inheritance does is used?
It is used in the following cases where hierarchy is to be maintained. For instance, the database of
an organization is stored in the hierarchical format. There are different sections of an organization
such as IT, computer science, Civil, Mechanical, etc. Each organization has the same attributes
such as student name, roll number, year, etc. which comes under a class Student. Hence all the
sections inherit the student properties and thus following the format of hierarchical inheritance.
Syntax :-
Class Parent
{
statement(s);
};
Class Derived1: public Parent
{
statement(s);
};
Class Derived2: public Parent
{
statement(s);
};
class newderived1: public Derived1
{
statement(s);
};
class newderived2: public Derived2
{
statement(s);
};
#include<iostream.h>
#include<conio.h>
class A
{
public :
void message()
{
cout<<"\n welcome to FYIT: ";
}
};
class B : public A
{
void display()
{
cout<<"\n Inside class B:";
}
};
class c : public A
{
public:
void putdata()
{
cout<<"\n Inside class c : "
}
};
int main()
{
B obj;
obj.display();
obj.message();
c obj1;
obj1.putdata();
obj1.message();
getch();
return 0;
}
Hybrid inheritance
Inheritance is defined as the process in which one class inherits the property of another class. The
class whose property is inherited is called as Base class or the parent of that class. The class that
inherits the base class's properties (parent class) is the derived class.
For example, A son inherits the properties of his father. This article will give you a brief
introduction to hybrid inheritance and its examples.
Definition
Combining various types of inheritance like multiple, simple, and hierarchical inheritance is
known as hybrid inheritance.
In simple inheritance, one class is derived from a single class which is its base. In multiple
inheritances, a class is derived from two classes, where one of the parents is also a derived class.
In hierarchical inheritance, more than one derived class is created from a single base class.
The diagram shows the hybrid inheritance that is a combination of single inheritance and multiple
inheritance.
Single inheritance - Class B inherits class A. Thus an example of single inheritance.
Multiple inheritance - Class D is inherited from multiple classes( B and C shown above D). Thus
an example of multiple inheritance.
Syntax code of the above example
Class A
{
statement(s)
}:
Class B: public A
{
statement(s);
};
Class C
{
statement(s);
};
Class D: public B, public C
{
statement(s);
};
#include<iostream.h>
#include<conio.h>
class A
{
public:
void putdata()
{
cout<<"\n Inside class A";
}
};
class B : public A
{
public:
void display()
{
cout<<"\n Inside class B";
}
};
class C
{
public:
void message()
{
cout<<"\n Inside class C";
}
};
class D : public B, public C
{
public:
void print()
{
cout<<"\n Inside class D";
}
};
int main()
{
D aa;
aa.print();
aa.display();
aa.message();
aa.putdata();
getch();
return 0;
}