You are on page 1of 37

EEN-103

PROGRAMMING IN C++

Object Oriented Programming Concepts: Data hiding, abstract data types,


classes, access control; Class implementation-default constructor, constructors,
copy constructor, destructor, operator overloading, friend functions; Introduction
to data structures, use of pointers in linked structures; Object oriented design (an
alternative to functional decomposition) inheritance and composition; Dynamic
binding and virtual functions; Polymorphism; Dynamic data in classes.
Types of Inheritance

1. Single 2. Multilevel 3. Hierarchical

4. Multiple 5. Hybrid
Visibility of Inherited members
Multiple Inheritance
• A class can inherit the attributes of two or more classes

• This is known as multiple inheritance

• Syntax of a derived class with multiple base classes :


class D: visibility B-1, visibility B-2, …..
{
………..
………… (Body of D)
…………
};
//Multiple Inheritance
#include <iostream>
using namespace std;
class M // First Base class
{ protected:int m;
public:void get_m(int);
};
class N // Second Base Class
{ protected:int n;
public:void get_n(int);
};
// Class P would contain all the members of class M & N
class P: public M, public N // Derived from multiple classess i.e M & N
{ public:void display(void);
};
void M::get_m(int x)
{ m=x;}
void N :: get_n(int y)
{ n=y;}
void P :: display(void)// mem. fun. of derived class P accessing protected members of M & N
{ cout<<"m= "<<m<<"\n";
cout<<"n= "<<n<<"\n";
cout<<"m*n= "<<m*n<<"\n";
}
int main()
{
P p; // Decralation of derived class object
p.get_m(10);// derived class object invoking mem. func of first base class M
p.get_n(20);// derived class object invoking mem. func of second base class N
p.display();//derived class object invoking mem. func of its own base class P
system("pause");return 0;}
Function overriding

• Derived class can override a base-class member function with


same signature
• It provides a new version of the function
• It is different from function overloading
– In overloading, two functions have the same name but different
parameter list
– In overriding, the functions have the same name and the same
parameter list
• For a derived class object, call to such a function invokes the
derived class version
• Original function can be invoked through scope resolution
operator
Multilevel Inheritance
• Class A serves as base class for derived class B

• Class B in turn serves as base class for class C

• Class B is called intermediate base class because it


provides a link for the inheritance between A and C

• The chain ABC is called as the inheritance path


• A derived class with Multilevel Inheritance is
declared as follows:

class A{……….}; // Base class


class B: public A {……}; // B derived from A
class C: public B {…….}; //C derived from B
Constructors
in
Derived Classes
What is not inherited from the base class?

•Constructors
•Destructors
•Non-member functions
•Assignment operators
•Virtual methods

If needed, they must be created


•Although the constructors and destructors of the
base class are not inherited themselves,

•its default constructor (i.e., its constructor with no


parameters) and

•its destructor are always called when a new object


of a derived class is created or destroyed.
•The header of derived-constructor function contains two parts
separated by a colon(:).

The first part provides the declaration of arguments that


are passed to the derived-constructor

The second part lists the function calls to the base


constructors

derived_constructor_name (parameters) : base_constructor_name (parameters)

Body of derived constructor

}
Example:
Constructor for derived class D derived from Class A and Class
B

D(int a1, int a2, float b1, float b2, int d1): A(a1, a2), B(b1,b2)
{
d = d1;
}

A(a1,a2) invokes base constructor A()


B(b1,b2) invokes another base constructor B()
Constructor D supplies the values for these four arguments
and fifth value is passed to its body
D obj(5,10,2.5,8.65,30);
Values assigned:
a1 -> 5 a2 -> 10 b1->2.5 b2->8.65 d->30
Derived class takes the responsibility of
supplying initial values to its base classes

We supply the initial values that are required


by all the classes together when a derived
class object is declared.

The constructor of the derived class receives


the entire list of values as its arguments and
passes them on to the base constructors in
the order in which they are declared in the
derived class.
The base constructors are called
and executed before executing
the statements in the body of the
derived constructors.
Order of execution of base class constructors

When both the derived and base


classes contain constructors,

•the base class constructor is


executed first

•then the constructor in the


derived class is executed
In case of multiple inheritance, base
classes are constructed in order in
which they appear in the declaration of
the derived class.

In case of multilevel inheritance, the


constructor will be executed in the
order of inheritance.
Example:
Multilevel Inheritance
Point, Circle, Cylinder
IS-A and HAS-A relationships
Composition
• describes a has-a relationship
• a class contains objects of some other classes as members
• e.g. an employee class may have Date of birth and phone
Number as members
Example
class employee {
private:
char name[30];
date Dob;
phone officePhone;
….
};
Composition

Constructor for employee class


employee::employee (char * s, int bd, int bm, int by, int std, int
number) : Dob(bd, bm, by), officePhone(std, number)
{
//code for setting the name part
}
• if a member initialiser is not provided, then member object’s
default constructor is called implicitly.
Summary of Inheritance
• Inheritance can be single or multiple or multilevel
• Three modes of inheritance – private, protected, public
Public Inheritance
• Written as class <derived> : public <base> { … } ;
• derived class cannot access private members of base class. It can
access them through public interface of base class.
• Protected members of base class are directly accessible by derived
class functions. They become protected for the derived class
• Public members of base are public members to the derived class
• Friends of base class are not inherited by derived class
• A derived class object is a base class object also. Hence a pointer to
a base class object can be made to point to a derived class object.
• Thus derived class and base class objects can be handled through a
common pointer
Public inheritance
Hierarchy of access in public inheritance
• Public of base – accessible by objects of derived
• Protected of base – accessible by public members and friends of
derived – protected allow propagation of inheritance
• Private of base – not accessible by derived; accessible through
public members and friends of base
Example
class point {
protected: int x, y;
public: point(int = 0, int = 0);
void setPoint(int, int);
int getX() const {return x;}
int getY() const {return y;}
friend ostream& operator<<(ostream&, point&);
};
Example contd…
ostream& operator<<(ostream& out, point& p) {
out << '[' << p.x << ", " << p.y << ']';
return out;
}
point::point(int a, int b) {
setPoint (a, b);
}
void point::setPoint (int a, int b) {
x = a;
y = b;
}
Example contd…
class circle : public point {
protected:
float radius;
public:
circle(float r = 0.0, int x = 0, int y = 0) ;
void setRadius(float);
float area() const;
float getRadius() const {return radius;}
friend ostream& operator<< (ostream& out, circle& c);
};
circle::circle (float r, int a, int b) : point(a,b) {
setRadius(r);
}
ostream& operator<<(ostream& out, circle& c) {
point a = static_cast <point> (c);
out << "Centre of the circle is at " << a << " and radius = " <<
c.radius;
return out; }
Using the base and derived classes
int main() {
point * pptr = 0, p(2, 3);
circle *cptr = 0, c(2.7, 4, 5);

cout << "point p: " << p << "\ncircle c: " << c << endl;

pptr = &c;
cout << "\nCircle c via pptr is: " << *pptr << endl;

cptr = static_cast <circle *> (pptr);


cout << "\nCircle c via cptr is: “ << *cptr << endl;
cin.get();
return 0;
}

You might also like