Professional Documents
Culture Documents
class Employee
// composition
{ private: string name; unsigned int number;
public:
void getdata()
{cout << Enter last name: ; cin >> name;
cout << Enter number: ; cin >> number;}
void putdata()
{cout << Name: << name << endl;
cout << Number: << number << endl;}
};
class Manager
{ private: string title; Employee emp;
public:
void getdata()
{emp.getdata();
cout << Enter title: ; cin >> title;}
void putdata()
{emp.putdata();
cout << Title: << title << endl;}
};
int main()
{ Manager m1;
m1.getdata();
m1.putdata();
}
Run
Enter last name: Daniel
Enter number: 14404
Enter title: AM
Name: Daniel
Number: 14404
Title: AM
Inheritance
Inheritance is the process of creating new classes from
the existing classes.
When creating a class, instead of writing it from scratch,
we can designate that the new class should inherit the
members of an existing class.
The existing class is called the base class, and the new
class is referred to as the derived class.
A derived class represents a more specialized group of
objects. It contains behaviors inherited from the base
class plus additional behaviors.
A derived class can also customize behaviors inherited
from the base class.
CommunityMember
Student
Employee
Single inheritance
Faculty
Administrator
Staff
Teacher
AdministratorTeacher
Single inheritance
Single inheritance
Multiple inheritance
Protected members:
Other than private and public access specifiers, C++
provides another access specifier, called protected.
protected access provides an intermediate level of
protection between private and public.
A base classs protected members can be accessed within
the body of that base class, by members and friends of
that base class, and by members and friends of any class
derived from that base class.
Derived class member functions can refer to public and
protected members of the base class simply by using the
member names.
class Counter
// inheritance (adding -- operator
{ protected:
// along with ++ operator)
unsigned int count;
public:
Counter(): count(0)
{
}
Counter(int c): count(c)
{
}
unsigned int get_count()
{return count; }
Counter operator ++ ()
{return Counter(++count);}
};
class CountDn: public Counter
//derived class CountDn
{ public:
CountDn(): Counter()
// derived class constructor
{
}
CountDn(int c): Counter(c) // derived class constructor
{
}
CountDn operator -- ()
{return CountDn(--count);}
};
int main()
{CountDn c1, c2(100);
cout << \nc1 = << c1.get_count();
cout << \nc2 = << c2.get_count();
++c1; ++c1; ++c1;
cout << \nc1 = << c1.get_count();
--c2; --c2;
cout << \nc2 = << c2.get_count();
CountDn c3 = --c2;
cout << \nc3 = << c3.get_count();
}
Output:
c1 = 0
c2 = 100
c1 = 3
c2 = 98
C3 = 97
class A
// function overriding
{ public:
void f()
{cout << A::f() is executing << endl;}
};
class B: public A
// derived class
{ public:
void f()
{cout << B::f() is executing << endl;
A::f();
// calling f()of A
}
};
int main()
{A a1; B b1;
a1.f();
b1.f();
}
Run
A::f() is executing
B::f() is executing
A::f() is executing
void getdist()
{Distance::getdist();
char ch;
cout << Enter sign (+ or -): ; cin >> ch;
sign = (ch ==+)? pos : neg;}
void showdist()
{cout << ((sign==pos) ? (+) : (-));
Distance::showdist();}
};
int main()
{DistSign alpha;
alpha.getdist();
DistSign beta(100, 6.25);
DistSign gamma(100, 5.5, neg);
cout << alpha = ; alpha.showdist();
cout << beta = ; beta.showdist();
cout << gamma = ; gamma.showdist();
}
Run:
Enter feet 10
Enter inches 4.5
Enter sign (+ or -): +
alpha = (+)10' - 4.5"
beta = (+)100' - 6.25"
gamma = (-)100' - 5.5
class B
{public:
B()
{cout <<Default constructor of the base-class is
executed" << endl;}
};
class D: public B
{public:
D()
{cout <<Default constructor of derived-class is
executed next" << endl;}
};
int main()
{D obj1;
}
Run:
Default constructor of the base-class is executed
Default constructor of derived-class is executed next
Multiple Inheritance:
A class can be derived from more than one base class.
This is called multiple inheritance.
The syntax for multiple inheritance is similar to that for
single inheritance.
class A
{
};
class B
{
};
class C: public A, public B
{
};
class Employee
// multiple inheritance
{ private: char name[50]; unsigned int number;
public:
void getdata()
{cout << Enter last name: ; cin >> name;
cout << Enter number: ; cin >> number;}
void putdata()
{cout << Name: << name << endl;
cout << Number: << number << endl;}
};
class Student
{ private: char degree[20];
public:
void getedu()
{cout << Enter highest degree earned : ;
cin >> degree;}
void putedu()
{cout << Highest degree earned:\n << degree ;}
};
class A
// parameter passing to constructors
{ public:
A(char a)
{cout << a <<endl;}
};
class B
{ public:
B(char b)
{cout << b <<endl;}
};
class C: public A, public B
{ public:
C(char c1, char c2, char c3): A(c1), B(c2)
{cout << c3 <<endl;}
};
Run:
int main()
a
{C objc(a, b, c);
b
}
c
Polymorphism
The term polymorphism is used in C++ to refer to the
form of a member function that can be changed at
runtime.
Such member functions are called virtual functions and
the corresponding class is called a polymorphic class.
The objects of the polymorphic class change at runtime
and respond differently for the same message.
Such a mechanism requires postponement of binding a
function call to the member function until runtime.
Example:
Suppose a graphic program includes several different shapes: a
triangle, a ball, a square, and so on. Also suppose that each of
these classes has a member function draw().
Now suppose we want to make a picture by grouping a number of
these elements together. One approach to do this is by creating an
array that holds pointers to all the different objects in the picture:
shape* ptrarr[100];
We can then draw the whole picture by using a simple loop:
for(i=0; i< n; i++)
ptrarr -> draw();
By this, completely different functions are executed by the same
function call.
For this approach to work, following conditions must be met:
1.
2.
All the different classes of shape must be derived from a single base class.
The draw() function must be declared to be virtual in the base class.
class Base
// Member functions accessed with pointers
{public:
void show()
{cout << Base << endl;}
};
class Derv1: public Base
{public:
void show()
{cout << Derv1 << endl;}
};
class Derv2: public Base
{public:
void show()
{cout << Derv2 << endl;}
};
int main()
{Derv1 dv1; Derv2 dv2;
Base* ptr;
ptr = &dv1;
ptr -> show();
ptr = &dv2;
ptr -> show();
}
Run:
Base
Base
//ptr->show() does the same work , irrespective
of the object it is working on.
class Base
// Virtual functions accessed with pointers
{public:
virtual void show()
{cout << Base << endl;}
};
class Derv1: public Base
{public:
void show()
{cout << Derv1 << endl;}
};
class Derv2: public Base
void show()
{public:
{cout << Derv2 << endl;}
};
int main()
{Derv1 dv1; Derv2 dv2;
Base* ptr;
ptr = &dv1;
ptr -> show();
ptr = &dv2;
ptr -> show();
}
Run:
Derv1
Derv2
//ptr->show() executes different functions,
depending on the content of the pointer.
class Graphics
// Member functions accessed with pointers
{public:
virtual void draw()
{cout << point << endl;}
};
class Triangle: public Graphics
{public:
void draw()
{cout << triangle << endl;}
};
class Rectangle: public Graphics
{public:
void draw()
{cout << rectangle << endl;}
};
class Circle: public Graphics
{public:
void draw()
{cout << circle << endl;}
};
int main()
{Graphics* gptrarr[4];
gptrarr[0]= new Graphics;
gptrarr[1]= new Triangle;
gptrarr[2]= new Rectangle;
gptrarr[3]= new Circle;
for(int i=0; i<4; i++)
{gptrarr[i] -> draw();}
}
Run:
point
triangle
rectangle
circle
class Graphics
// Member functions accessed with pointers
{public:
virtual void draw() = 0;
};
class Triangle: public Graphics
{public:
void draw()
{cout << "triangle" << endl;}
};
class Rectangle: public Graphics
{public:
void draw()
{cout << "rectangle" << endl;}
};
class Circle: public Graphics
{public:
void draw()
{cout << "circle" << endl;}
};
int main()
{
Graphics* gptrarr[3];
gptrarr[0]= new Triangle;
gptrarr[1]= new Rectangle;
gptrarr[2]= new Circle;
for(int i=0; i<3; i++)
{gptrarr[i] -> draw();
}
Run:
triangle
rectangle
circle