Examples of inheritance by using c++
#include <iostream. h>
class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
};
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
};
class Output {
public:
static void print (int i);
};
public:
static void print (int i);
};
void Output::print (int i) {
cout << i << '\n';
}
cout << i << '\n';
}
class Rectangle: public Polygon, public Output {
public:
Rectangle (int a, int b) : Polygon(a,b) {}
int area ()
{ return width*height; }
};
public:
Rectangle (int a, int b) : Polygon(a,b) {}
int area ()
{ return width*height; }
};
class Triangle: public Polygon, public Output {
public:
Triangle (int a, int b) : Polygon(a,b) {}
int area ()
{ return width*height/2; }
};
int main () {
Rectangle rect (4,5);
Triangle trgl (4,5);
rect.print (rect.area());
Triangle::print (trgl.area());
return 0;
}
public:
Triangle (int a, int b) : Polygon(a,b) {}
int area ()
{ return width*height/2; }
};
int main () {
Rectangle rect (4,5);
Triangle trgl (4,5);
rect.print (rect.area());
Triangle::print (trgl.area());
return 0;
}
// derived classes
#include <iostream. h>
#include <iostream. h>
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class Rectangle: public Polygon {
public:
int area ()
{ return width * height; }
};
public:
int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
int area ()
{ return width * height / 2; }
};
int main () {
Rectangle rect;
Triangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
public:
int area ()
{ return width * height / 2; }
};
int main () {
Rectangle rect;
Triangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << '\n';
cout << trgl.area() << '\n';
return 0;
}
// constructors and derived classes
#include <iostream. h>
class Mother {
public:
Mother ()
{ cout << "Mother: no parameters\n"; }
Mother (int a)
{ cout << "Mother: int parameter\n"; }
};
class Daughter : public Mother {
public:
Daughter (int a)
{ cout << "Daughter: int parameter\n\n"; }
};
class Son : public Mother {
public:
Son (int a) : Mother (a)
{ cout << "Son: int parameter\n\n"; }
};
int main () {
Daughter kelly(0);
Son bud(0);
return 0;
}
#include <iostream. h>
class Mother {
public:
Mother ()
{ cout << "Mother: no parameters\n"; }
Mother (int a)
{ cout << "Mother: int parameter\n"; }
};
class Daughter : public Mother {
public:
Daughter (int a)
{ cout << "Daughter: int parameter\n\n"; }
};
class Son : public Mother {
public:
Son (int a) : Mother (a)
{ cout << "Son: int parameter\n\n"; }
};
int main () {
Daughter kelly(0);
Son bud(0);
return 0;
}
Source Code to Implement Inheritance in C++ Programming
This example calculates the area and perimeter a rectangle using the concept of inheritance.
This example calculates the area and perimeter a rectangle using the concept of inheritance.
/* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */
#include <iostream>
using namespace std;
class Rectangle
{
protected:
float length, breadth;
public:
Rectangle(): length(0.0), breadth(0.0)
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}
using namespace std;
class Rectangle
{
protected:
float length, breadth;
public:
Rectangle(): length(0.0), breadth(0.0)
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}
};
/* Area class is derived from base class Rectangle. */
class Area : public Rectangle
{
public:
float calc()
{
return length*breadth;
}
class Area : public Rectangle
{
public:
float calc()
{
return length*breadth;
}
};
/* Perimeter class is derived from base class Rectangle. */
class Perimeter : public Rectangle
{
public:
float calc()
{
return 2*(length+breadth);
}
};
class Perimeter : public Rectangle
{
public:
float calc()
{
return 2*(length+breadth);
}
};
int main()
{
cout<<"Enter data for first rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.calc()<<" square meter\n\n";
{
cout<<"Enter data for first rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.calc()<<" square meter\n\n";
cout<<"Enter data for second rectangle to find perimeter.\n";
Perimeter p;
cout<<"\nPerimeter = "<<p.calc()<<" meter";
return 0;
}
Output
Perimeter p;
cout<<"\nPerimeter = "<<p.calc()<<" meter";
return 0;
}
Output
Enter data for first rectangle to find area.
Enter length: 5
Enter breadth: 4
Area = 20 square meter
Enter length: 5
Enter breadth: 4
Area = 20 square meter
Enter data for second rectangle to find perimeter.
Enter length: 3
Enter breadth: 2
Area = 10 meter
Enter length: 3
Enter breadth: 2
Area = 10 meter
// counten.cpp
// inheritance with Counter class
#include <iostream. h>
// inheritance with Counter class
#include <iostream. h>
////////////////////////////////////////////////////////////////
class Counter //base class
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count(0) //no-arg constructor
{ }
Counter(int c) : count(c) //1-arg constructor
{ }
unsigned int get_count() const //return count
{ return count; }
Counter operator ++ () //incr count (prefix)
{ return Counter(++count); }
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter //derived class
{
public:
Counter operator -- () //decr count (prefix)
{ return Counter(--count); }
};
////////////////////////////////////////////////////////////////
int main()
{
CountDn c1; //c1 of class CountDn
cout << “\nc1=” << c1.get_count(); //display c1
++c1; ++c1; ++c1; //increment c1, 3 times
cout << “\nc1=” << c1.get_count(); //display it
--c1; --c1; //decrement c1, twice
cout << “\nc1=” << c1.get_count(); //display it
cout << endl;
return 0;
}
class Counter //base class
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count(0) //no-arg constructor
{ }
Counter(int c) : count(c) //1-arg constructor
{ }
unsigned int get_count() const //return count
{ return count; }
Counter operator ++ () //incr count (prefix)
{ return Counter(++count); }
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter //derived class
{
public:
Counter operator -- () //decr count (prefix)
{ return Counter(--count); }
};
////////////////////////////////////////////////////////////////
int main()
{
CountDn c1; //c1 of class CountDn
cout << “\nc1=” << c1.get_count(); //display c1
++c1; ++c1; ++c1; //increment c1, 3 times
cout << “\nc1=” << c1.get_count(); //display it
--c1; --c1; //decrement c1, twice
cout << “\nc1=” << c1.get_count(); //display it
cout << endl;
return 0;
}
// counten2.cpp
// constructors in derived class
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count() //constructor, no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{ return count; }
Counter operator ++ () //incr count (prefix)
{ return Counter(++count); }
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter
{
public:
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c) : Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{ return CountDn(--count); }
};
////////////////////////////////////////////////////////////////
int main()
{
CountDn c1; //class CountDn
CountDn c2(100);
cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count(); //display
++c1; ++c1; ++c1; //increment c1
cout << “\nc1=” << c1.get_count(); //display it
--c2; --c2; //decrement c2
cout << “\nc2=” << c2.get_count(); //display it
CountDn c3 = --c2; //create c3 from c2
cout << “\nc3=” << c3.get_count(); //display c3
cout << endl;
return 0;
}
This program uses two new constructors in the CountDn class. Here is the no-argument constructor:
CountDn() : Counter()
{ }
// constructors in derived class
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count() //constructor, no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{ return count; }
Counter operator ++ () //incr count (prefix)
{ return Counter(++count); }
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter
{
public:
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c) : Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{ return CountDn(--count); }
};
////////////////////////////////////////////////////////////////
int main()
{
CountDn c1; //class CountDn
CountDn c2(100);
cout << “\nc1=” << c1.get_count(); //display
cout << “\nc2=” << c2.get_count(); //display
++c1; ++c1; ++c1; //increment c1
cout << “\nc1=” << c1.get_count(); //display it
--c2; --c2; //decrement c2
cout << “\nc2=” << c2.get_count(); //display it
CountDn c3 = --c2; //create c3 from c2
cout << “\nc3=” << c3.get_count(); //display c3
cout << endl;
return 0;
}
This program uses two new constructors in the CountDn class. Here is the no-argument constructor:
CountDn() : Counter()
{ }
// staken.cpp
// overloading functions in base and derived classes
#include <iostream>
using namespace std;
#include <process.h> //for exit()
////////////////////////////////////////////////////////////////
class Stack
{
protected: //NOTE: can’t be private
enum { MAX = 3 }; //size of stack array
int st[MAX]; //stack: array of integers
int top; //index to top of stack
public:
Stack() //constructor
{ top = -1; }
void push(int var) //put number on stack
{ st[++top] = var; }
int pop() //take number off stack
{ return st[top--]; }
};
////////////////////////////////////////////////////////////////
class Stack2 : public Stack
{
public:
void push(int var) //put number on stack
{
if(top >= MAX-1) //error if stack full
{ cout << “\nError: stack is full”; exit(1); }
Stack::push(var); //call push() in Stack class
}
int pop() //take number off stack
{
if(top < 0) //error if stack empty
{ cout << “\nError: stack is empty\n”; exit(1); }
return Stack::pop(); //call pop() in Stack class
}
};
////////////////////////////////////////////////////////////////
int main()
{
Stack2 s1;
s1.push(11); //push some values onto stack
s1.push(22);
s1.push(33);
cout << endl << s1.pop(); //pop some values from stack
cout << endl << s1.pop();
cout << endl << s1.pop();
cout << endl << s1.pop(); //oops, popped one too many...
cout << endl;
return 0;
}
In this program the Stack class is just the same as it was in the STAKARAY program, except that
the data members have been made protected.
// overloading functions in base and derived classes
#include <iostream>
using namespace std;
#include <process.h> //for exit()
////////////////////////////////////////////////////////////////
class Stack
{
protected: //NOTE: can’t be private
enum { MAX = 3 }; //size of stack array
int st[MAX]; //stack: array of integers
int top; //index to top of stack
public:
Stack() //constructor
{ top = -1; }
void push(int var) //put number on stack
{ st[++top] = var; }
int pop() //take number off stack
{ return st[top--]; }
};
////////////////////////////////////////////////////////////////
class Stack2 : public Stack
{
public:
void push(int var) //put number on stack
{
if(top >= MAX-1) //error if stack full
{ cout << “\nError: stack is full”; exit(1); }
Stack::push(var); //call push() in Stack class
}
int pop() //take number off stack
{
if(top < 0) //error if stack empty
{ cout << “\nError: stack is empty\n”; exit(1); }
return Stack::pop(); //call pop() in Stack class
}
};
////////////////////////////////////////////////////////////////
int main()
{
Stack2 s1;
s1.push(11); //push some values onto stack
s1.push(22);
s1.push(33);
cout << endl << s1.pop(); //pop some values from stack
cout << endl << s1.pop();
cout << endl << s1.pop();
cout << endl << s1.pop(); //oops, popped one too many...
cout << endl;
return 0;
}
In this program the Stack class is just the same as it was in the STAKARAY program, except that
the data members have been made protected.
// pubpriv.cpp
// tests publicly- and privately-derived classes
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class A //base class
{
private:
int privdataA; //(functions have the same access
protected: //rules as the data shown here)
int protdataA;
public:
int pubdataA;
};
////////////////////////////////////////////////////////////////
class B : public A //publicly-derived class
{
public:
void funct()
{
int a;
a = privdataA; //error: not accessible
a = protdataA; //OK
a = pubdataA; //OK
}
};
////////////////////////////////////////////////////////////////
class C : private A //privately-derived class
{
public:
void funct()
{
int a;
a = privdataA; //error: not accessible
a = protdataA; //OK
a = pubdataA; //OK
}
};
////////////////////////////////////////////////////////////////
int main()
{
int a;
B objB;
a = objB.privdataA; //error: not accessible
a = objB.protdataA; //error: not accessible
a = objB.pubdataA; //OK (A public to B)
C objC;
a = objC.privdataA; //error: not accessible
a = objC.protdataA; //error: not accessible
a = objC.pubdataA; //error: not accessible (A private to C)
return 0;
}
// tests publicly- and privately-derived classes
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class A //base class
{
private:
int privdataA; //(functions have the same access
protected: //rules as the data shown here)
int protdataA;
public:
int pubdataA;
};
////////////////////////////////////////////////////////////////
class B : public A //publicly-derived class
{
public:
void funct()
{
int a;
a = privdataA; //error: not accessible
a = protdataA; //OK
a = pubdataA; //OK
}
};
////////////////////////////////////////////////////////////////
class C : private A //privately-derived class
{
public:
void funct()
{
int a;
a = privdataA; //error: not accessible
a = protdataA; //OK
a = pubdataA; //OK
}
};
////////////////////////////////////////////////////////////////
int main()
{
int a;
B objB;
a = objB.privdataA; //error: not accessible
a = objB.protdataA; //error: not accessible
a = objB.pubdataA; //OK (A public to B)
C objC;
a = objC.privdataA; //error: not accessible
a = objC.protdataA; //error: not accessible
a = objC.pubdataA; //error: not accessible (A private to C)
return 0;
}
class A // base class A
{
};
class B // base class B
{
};
class C : public A, public B // C is derived from A and B
{
};
{
};
class B // base class B
{
};
class C : public A, public B // C is derived from A and B
{
};
Here’s an example, AMBIGU, that demonstrates the situation:
// ambigu.cpp
// demonstrates ambiguity in multiple inheritance
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class A
{
public:
void show() { cout << “Class A\n”; }
};
class B
{
public:
void show() { cout << “Class B\n”; }
};
class C : public A, public B
{
};
////////////////////////////////////////////////////////////////
int main()
{
C objC; //object of class C
// objC.show(); //ambiguous--will not compile
objC.A::show();
// ambigu.cpp
// demonstrates ambiguity in multiple inheritance
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class A
{
public:
void show() { cout << “Class A\n”; }
};
class B
{
public:
void show() { cout << “Class B\n”; }
};
class C : public A, public B
{
};
////////////////////////////////////////////////////////////////
int main()
{
C objC; //object of class C
// objC.show(); //ambiguous--will not compile
objC.A::show();
ادعو لوالدتي جزاكم الله خيرا
HamedDiuala
HamedDiuala
Examples of inheritance by using c++
Reviewed by حامد طالب العراقي
on
5/16/2015 10:09:00 ص
Rating:
ليست هناك تعليقات: