A multilevel hierarchy
#include <iostream>
#include <cstring>
using namespace std;
class Shape {
// private
double width;
double height;
public:
Shape() {
width = height = 0.0;
}
Shape(double w, double h) {
width = w;
height = h;
}
Shape(double x) {
width = height = x;
}
void display() {
cout << "Width and height are " << width << " and " << height << "\n";
}
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
};
class Triangle : public Shape {
char style[20];
public:
/* This automatically invokes the default constructor of Shape. */
Triangle() {
strcpy(style, "unknown");
}
Triangle(char *str, double w, double h) : Shape(w, h) {
strcpy(style, str);
}
Triangle(double x) : Shape(x) {
strcpy(style, "isosceles");
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
class NameTriangle : public Triangle {
char name[20];
public:
NameTriangle(char *clr, char *style, double w, double h) : Triangle(style, w,
h) {
strcpy(name, clr);
}
void displayName() {
cout << "Name is " << name << "\n";
}
};
int main() {
NameTriangle t1("A", "right", 8.0, 12.0);
NameTriangle t2("B", "isosceles", 2.0, 2.0);
t1.showStyle();
t1.display();
t1.displayName();
cout << "Area is " << t1.area() << "\n";
t2.showStyle();
t2.display();
t2.displayName();
cout << "Area is " << t2.area() << "\n";
return 0;
}
Triangle is right
Width and height are 8 and 12
Name is A
Area is 48
Triangle is isosceles
Width and height are 2 and 2
Name is B
Area is 2
A simple class hierarchy.
#include <iostream>
#include <cstring>
using namespace std;
class Shape {
public:
double width;
double height;
void display() {
cout << "Width and height are " << width << " and " << height << "\n";
}
};
// Triangle is derived from Shape.
class Triangle : public Shape {
public:
char style[20];
double area() {
return width * height / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
int main() {
Triangle t1;
Triangle t2;
t1.width = 4.0;
t1.height = 4.0;
strcpy(t1.style, "isosceles");
t2.width = 8.0;
t2.height = 12.0;
strcpy(t2.style, "right");
t1.showStyle();
t1.display();
cout << "Area is " << t1.area() << "\n";
t2.showStyle();
t2.display();
cout << "Area is " << t2.area() << "\n";
return 0;
}
Triangle is isosceles
Width and height are 4 and 4
Area is 8
Triangle is right
Width and height are 8 and 12
Area is 48
Call constructor from base class
#include <iostream>
using namespace std;
class MyType {
protected:
float value;
public:
float subtract(float);
float add(float);
void display();
MyType(float);
MyType();
};
MyType::MyType(float num)
{
value = num;
}
MyType::MyType()
{
value = 1000;
}
float MyType::subtract(float amount)
{
value -= amount;
return value;
}
float MyType::add(float amount)
{
value += amount;
return value;
}
void MyType::display()
{
cout << "Your value is " << value << endl;
}
class DerivedType:public MyType
{
public:
DerivedType(float);
};
DerivedType::DerivedType(float num):MyType(num)
{
}
DerivedType myType(1200);
int main()
{
float amount, balance;
myType.display();
cout << endl;
cout << "Please enter the amount to deposit \n";
cin>> amount;
balance = myType.add(amount);
cout << "New balance is " << balance << endl;
cout << endl;
cout << "Please enter the amount to withdraw \n";
cin >> amount;
balance = myType.subtract(amount);
cout << "New balance is " << balance << endl;
cout << endl;
return 0;
}
Your value is 1200
Please enter the amount to deposit
123
New balance is 1323
Please enter the amount to withdraw
123
New balance is 1200
Deriving ADTs from other ADTs
/*
Quote from
Teach Yourself C++ in 24 Hours, 4th Edition
Publisher: Sams; 4 edition (August 11, 2004)
Language: English
ISBN-10: 0672326817
ISBN-13: 978-0672326813
by Jesse Liberty (Author), David Horvath (Author)
*/
#include <iostream>
enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
class Animal // common base to both horse and bird
{
public:
Animal(int);
virtual ~Animal() {
std::cout << "Animal destructor...\n";
}
virtual int GetAge() const {
return itsAge;
}
virtual void SetAge(int age) {
itsAge = age;
}
virtual void Sleep() const = 0;
virtual void Eat() const = 0;
virtual void Reproduce() const = 0;
virtual void Move() const = 0;
virtual void Speak() const = 0;
private:
int itsAge;
};
Animal::Animal(int age):itsAge(age)
{
std::cout << "Animal constructor...\n";
}
class Mammal : public Animal
{
public:
Mammal(int age):Animal(age) {
std::cout << "Mammal constructor...\n";
}
virtual ~Mammal() {
std::cout << "Mammal destructor...\n";
}
virtual void Reproduce() const {
std::cout << "Mammal reproduction depicted...\n";
}
};
class Fish : public Animal
{
public:
Fish(int age):Animal(age) {
std::cout << "Fish constructor...\n";
}
virtual ~Fish() {
std::cout << "Fish destructor...\n";
}
virtual void Sleep() const {
std::cout << "fish snoring...\n";
}
virtual void Eat() const {
std::cout << "fish feeding...\n";
}
virtual void Reproduce() const {
std::cout << "fish laying eggs...\n";
}
virtual void Move() const {
std::cout << "fish swimming...\n";
}
virtual void Speak() const { }
};
class Horse : public Mammal
{
public:
Horse(int age, COLOR color ): Mammal(age), itsColor(color) {
std::cout << "Horse constructor...\n";
}
virtual ~Horse() {
std::cout << "Horse destructor...\n";
}
virtual void Speak()const {
std::cout << "Whinny!... \n";
}
virtual COLOR GetItsColor() const
{ return itsColor; }
virtual void Sleep() const
{ std::cout << "Horse snoring...\n"; }
virtual void Eat() const
{ std::cout << "Horse feeding...\n"; }
virtual void Move() const
{ std::cout << "Horse running...\n";}
protected:
COLOR itsColor;
};
class Dog : public Mammal
{
public:
Dog(int age, COLOR color ):
Mammal(age), itsColor(color)
{ std::cout << "Dog constructor...\n"; }
virtual ~Dog()
{ std::cout << "Dog destructor...\n"; }
virtual void Speak()const
{ std::cout << "Whoof!... \n"; }
virtual void Sleep() const
{ std::cout << "Dog snoring...\n"; }
virtual void Eat() const
{ std::cout << "Dog eating...\n"; }
virtual void Move() const
{ std::cout << "Dog running...\n"; }
virtual void Reproduce() const
{ std::cout << "Dogs reproducing...\n"; }
protected:
COLOR itsColor;
};
int main()
{
Animal *pAnimal=0;
pAnimal = new Dog(5,Brown);
pAnimal->Speak();
pAnimal->Eat();
pAnimal->Reproduce();
pAnimal->Move();
pAnimal->Sleep();
delete pAnimal;
pAnimal = new Horse(4,Black);
pAnimal->Speak();
pAnimal->Eat();
pAnimal->Reproduce();
pAnimal->Move();
pAnimal->Sleep();
delete pAnimal;
pAnimal = new Fish (5);
pAnimal->Speak();
pAnimal->Eat();
pAnimal->Reproduce();
pAnimal->Move();
pAnimal->Sleep();
delete pAnimal;
return 0;
}
Animal constructor...
Mammal constructor...
Dog constructor...
Whoof!...
Dog eating...
Dogs reproducing...
Dog running...
Dog snoring...
Dog destructor...
Mammal destructor...
Animal destructor...
Animal constructor...
Mammal constructor...
Horse constructor...
Whinny!...
Horse feeding...
Mammal reproduction depicted...
Horse running...
Horse snoring...
Horse destructor...
Mammal destructor...
Animal destructor...
Animal constructor...
Fish constructor...
fish feeding...
fish laying eggs...
fish swimming...
fish snoring...
Fish destructor...
Animal destructor...
Inherit protected fields
#include <iostream>
using namespace std;
class MyType {
protected:
float value;
public:
float remove(float);
float add(float);
void display();
MyType();
};
MyType::MyType()
{
value = 1000;
}
float MyType::remove(float amount)
{
value -= amount;
return value;
}
float MyType::add(float amount)
{
value += amount;
return value;
}
void MyType::display()
{
cout << "Your value is " << value << endl;
}
class DerivedType:public MyType
{
};
DerivedType myDerivedType;
int main()
{
float amount, value;
myDerivedType.display();
cout << endl;
cout << "Please enter the amount to add \n";
cin>> amount;
value = myDerivedType.add(amount);
cout << "New value is " << value << endl;
cout << endl;
cout << "Please enter the amount to remove \n";
cin >> amount;
value = myDerivedType.remove(amount);
cout << "New value is " << value << endl;
cout << endl;
return 0;
}
Your value is 1000
Please enter the amount to add
123
New value is 1123
Please enter the amount to remove
123
New value is 1000
Multiple base class constructing and destructing
#include <iostream>
using namespace std;
class base1 {
public:
base1() {
cout << "Constructing base1\n";
}
~base1() {
cout << "Destructing base1\n";
}
};
class base2 {
public:
base2() {
cout << "Constructing base2\n";
}
~base2() {
cout << "Destructing base2\n";
}
};
class derived: public base1, public base2 {
public:
derived() {
cout << "Constructing derived\n";
}
~derived() {
cout << "Destructing derived\n";
}
};
int main()
{
derived ob;
return 0;
}
Constructing base1
Constructing base2
Constructing derived
Destructing derived
Destructing base2
Destructing base1
Overload across class hiearchy
class Base {
public:
int f(int i) { return (i*2); }
float f(float f) { return (f*2);}
};
class Derived: public Base {
public:
int f(int i, int j) { return (i +j); }
};
int main()
{
Derived var;
float f;
int i;
i = var.f(3, 5); // Works
//f = var.f(3.0); // Fails
return (0);
}
Reference another class in member function
#include <iostream>
using namespace std;
class ClassA {
public:
void functionA();
};
void ClassA::functionA()
{
cout << "This is a function in ClassA \n";
}
class ClassB {
public:
void functionB();
};
void ClassB::functionB() {
ClassA myclass;
myclass.functionA();
}
int main() {
ClassB myclass;
ClassA anotherclass;
myclass.functionB();
anotherclass.functionA();
return 0;
}
This is a function in ClassA
This is a function in ClassA
Shadow base class function in its two children classes
#include <iostream>
#include <stdlib.h>
using namespace std;
class BaseClass {
public:
void f(float);
};
void BaseClass::f(float price)
{
cout << "the base class!\n";
cout << price * .075f;
}
class DerivedClass1:public BaseClass
{
public:
void f(float);
};
void DerivedClass1::f(float price)
{
cout << "the child class 1\n";
cout << price * .010f << endl;
}
class DerivedClass2:public BaseClass
{
public:
void f(float);
};
void DerivedClass2::f(float price)
{
cout << "the child class 2\n";
cout << price * .005f << endl;
}
int main()
{
DerivedClass1 myclass;
DerivedClass2 anotherclass;
myclass.f(100);
anotherclass.f(100);
return 0;
}
the child class 1
1
the child class 2
0.5
Shadow function with the same name in base class
#include <iostream>
#include <stdlib.h>
using namespace std;
class BaseClass
{
public:
void f();
};
void BaseClass::f()
{
cout << "Hey this is the base class!\n";
}
class DerivedClass:public BaseClass
{
public:
void f();
};
void DerivedClass::f()
{
cout << "Hey this is in the child class!!\n";
}
int main()
{
DerivedClass myclass;
myclass.f();
return 0;
}
Hey this is in the child class!!
Three level inheritance
#include <iostream>
using namespace std;
class base {
public:
base() {
cout << "Constructing base\n";
}
~base() {
cout << "Destructing base\n";
}
};
class derived1 : public base {
public:
derived1() {
cout << "Constructing derived1\n";
}
~derived1() {
cout << "Destructing derived1\n";
}
};
class derived2: public derived1 {
public:
derived2() {
cout << "Constructing derived2\n";
}
~derived2() {
cout << "Destructing derived2\n";
}
};
int main()
{
derived2 ob;
return 0;
}
Constructing base
Constructing derived1
Constructing derived2
Destructing derived2
Destructing derived1
Destructing base
Use typeid to test type equality
#include <iostream>
#include <typeinfo>
using namespace std;
class Base {};
class Derived : public Base {};
int main( ) {
Base b, bb;
Derived d;
if (typeid(b) == typeid(d)) { // No
cout << "b and d are of the same type.\n";
}
if (typeid(b) == typeid(bb)) { // Yes
cout << "b and bb are of the same type.\n";
}
if (typeid(d) == typeid(Derived)) { // Yes
cout << "d is of type Derived.\n";
}
}
b and bb are of the same type.
d is of type Derived.