/* C++ Class Inheritance, Polymorphism and Friend Demo

Copyright © Robin Broad 2018

Free software published under GNU GPL

http://www.robinbroad.co.uk/

May 2018

*/

 

//Preprocessor instructions to the C++ compiler

//Include the standard C input/ output library

#include <iostream>

//Use the standard names in namespace std for the functions in the iostream library (e.g. cout). Without this, the compiler would not recognise the function cout, which we are using in this program

using namespace std;

 

//Base class

class Material

/*Class name Material. This class is designed to be a base class for the subclasses Plastic, Metal and Ceramic. These subclasses are all materials and inherit some common properties from their parent (base) class Material.

This code is heavily commented to assist students of C++

Author: Robin Broad

C++ Examples

http://www.robinbroad.co.uk/

May 2018

*/

{

        protected:

        /*To make these class members accessible to subclasses (derived classes) these variables need to be declared as protected, not private. (This is also known as data hiding. This is part of the process of encapsulation; information hiding, data and methods)*/

        float density;

        //Material density in Kg/m^3

        float cost;

        //Object cost in UK £

        public:

        //***Constructor ***

        /*The constructor is a member function with the same name as the class.

        It is called whenever an object (class instance) is created*/

        Material(float fpDensity, float fpCost){

        //fp means function parameter etc.

                density=fpDensity;

                cost=fpCost;

        }

 

        void properties()

        /*Member function to display the general properties of the base class Material

        This member function is overridden by the derived classes Plastic, Metal and Ceramic

        Void means that this function does not return a value*/

        {

                cout << "I am a material. I am a solid and can be used in manufacturing.\n"<<"Density: "<<density<<"Kg/m^3\nCost: £"<<cost << "\n\n";

        }

 

        /*Declaring the friend function twoPoundsOff

        This function deducts £2 from the cost of the object because it is being discounted for a sale

        The class member cost is protected in this class, but because this function is a friend, it is authorised to change the class member

        The function twoPoundsOff is not a member of the Material class

        We are passing a pointer to the Material object (rather than a copy) so that it's instance member can be modified

        Void means that this function does not return a value*/

        friend void twoPoundsOff(Material *poA);

};

//}; marks the end of the class definition

 

/*First derived class

Plastic is a type of material*/

class Plastic : public Material

/*Class name Plastic. This class is derived from the parent (base) class Material. It inherits some of the properties of the base class Material.*/

{

        public:

        //***Constructor ***

        /*The constructor is a member function with the same name as the class.

        It is called whenever an object (class instance) is created

        This constructor is inherited from the base class*/

        Plastic(float a, float b):Material(a,b){}

       

        /*Function overriding

        The definition of the properties() function in the base class is overridden here*/

        void properties()

        {

                cout << "I am a plastic object. I am strong and hard wearing, I can be moulded and I am and electrical and heat insulator.\n"<<"Density: "<<density<<"Kg/m^3\nCost: £"<<cost << "\n\n";

        }

       

};

//}; marks the end of the class definition

 

/*Second derived class

metal is a type of material*/

class Metal : public Material

/*Class name Metal. This class is derived from the parent (base) class Material. It inherits some of the properties of the base class Material.*/

{

        public:

        //***Constructor ***

        /*The constructor is a member function with the same name as the class.

        It is called whenever an object (class instance) is created

        This constructor is inherited from the base class*/

        Metal(float a, float b):Material(a,b){}

       

        /*Function overriding

        The definition of the properties() function in the base class is overridden here*/

        void properties()

        {

                cout << "I am a metal object. I am strong, hard and shiny, and I am a good conductor of heat and electricity.\n"<<"Density: "<<density<<"Kg/m^3\nCost: £"<<cost << "\n\n";

        }

       

};

//}; marks the end of the class definition

 

/*Third derived class

Ceramic is a type of material*/

class Ceramic : public Material

/*Class name Ceramic. This class is derived from the parent (base) class Material. It inherits some of the properties of the base class Material.*/

{

        public:

        //***Constructor ***

        /*The constructor is a member function with the same name as the class.

        It is called whenever an object (class instance) is created

        This constructor is inherited from the base class*/

        Ceramic(float a, float b):Material(a,b){}

       

        /*Function overriding

        The definition of the properties() function in the base class is overridden here*/

        void properties()

        {

                cout << "I am a ceramic object. I am hard wearing but brittle, heat resistant and I am an electrical and heat insulator.\n"<<"Density: "<<density<<"Kg/m^3\nCost: £"<<cost << "\n\n";

        }

       

};

//}; marks the end of the class definition

 

/*Base class friend function

Applies a two pound discount to the object

This function is not a member of the class Material*/

void twoPoundsOff(Material *poA)

//We are passing a pointer to the Material object (rather than a copy) so that it's instance member can be modified*/

{

        (*poA).cost-=2;

        //(*poA) means the object that is pointed to by *poA

        //An alternative syntax is poA->cost-=2

        //x-=2 means x=x-2

}

/*We could have passed the object by reference, rather than using a pointer.

The syntax is as follows:

void twoPoundsOff(Material &poA){ poA.cost-=2;}

{Where & is the reference operator)

This time the member function would be called as follows:

twoPoundsOff(objectName); instead of twoPoundsOff(&objectName);

*/

 

//*** Main program ***

int main()

{

        //Output Header

        cout <<"C++ Class Inheritance, Polymorphism and Friend Demo\nRobin Broad\nMay 2018\n";

        /*This program demonstrates class inheritance, polymorphism and friend functions

        Inheritance - The subclasses Plastic, Metal and Ceramic are all materials and inherit some common properties from their parent (base) class Material

        Polymorphism - From the Greek meaning "many forms" this is an important principle of Object Orientated Programming. The derived classes can exhibit different forms of behaviour depending upon their properties. In this case, the properties member function is overridden in the derived classes. Also, the friend function twoPoundsOff(Material *poA) is able to operate on objects of the base class or derived classes, another example of Polymorphism

        Friend function - The class member cost is protected in the Material base class, the function twoPoundsOff(Material *poA)is a friend, it is authorised to change the class member

        Abstraction - This is the concept of simplifying real world things and representing their essential characteristics in software. For example, the plastic toy is represented as a plastic object with the properties of plastic, a density and a cost.

        Classification - The materials plastic, metal and ceramic are grouped together as members of the class material*/

 

        /*Create an object of the base class Material

        This wooden table is made from pine (density 420Kg/m^3) and costs £49.99*/

        Material woodenTable(420,49.99);

       

        /*Create an object of the derived class Plastic

        This toy is made from plastic (density 920Kg/m^3) and costs £3.50*/

        Plastic plasticToy(920,3.50);

        /*Create an object of the derived class Metal

        This saucepan is made from stainless steel (density 8000Kg/m^3) and costs £7.50*/

        Metal saucepan(8000,7.50);

 

        /*Create an object of the derived class Ceramic

        This teacup is made from ceramic (density 2130Kg/m^3) and costs £2.25*/

        Ceramic teaCup(2130,2.25);

       

        /*Display the properties of the wooden table

        The base class (Material) properties function is called*/

        cout<<"Properties of the wooden table: ";

        woodenTable.properties();

       

        /*Display the properties of the plastic toy

        The derived class Plastic overrides the properties function in the base class Material

        The plastic class properties function is called*/

        cout<<"Properties of the plastic toy: ";

        plasticToy.properties();

 

        /*Display the properties of the saucepan

        The derived class Metal overrides the properties function in the base class Material

        The Metal class properties function is called*/

        cout<<"Properties of the saucepan: ";

        saucepan.properties();

 

        /*Display the properties of the teacup

        The derived class Ceramic overrides the properties function in the base class Material

        The Ceramic class properties function is called*/

        cout<<"Properties of the teacup: ";

        teaCup.properties();

 

        //Discount the wooden table using a friend function

        twoPoundsOff(&woodenTable);

        //&woodenTable means the address of the object woodenTable

        cout<<"Properties of the discounted wooden table: ";

        woodenTable.properties();

 

        //Discount the plastic toy using a friend function

        twoPoundsOff(&plasticToy);

        //&plasticToy means the address of the object plasticToy

        cout<<"Properties of the discounted plastic toy: ";

        plasticToy.properties();

 

        return 0;

}

 

/*This program produced the following output when compiled using the GNU GCC C++ compiler from within the Code::Blocks IDE running on an Ubuntu GNU/Linux operating system, by Robin Broad, Newcastle upon Tyne, UK, May 2018:

 

C++ Class Inheritance, Polymorphism and Friend Demo

Robin Broad

May 2018

Properties of the wooden table: I am a material. I am a solid and can be used in manufacturing.

Density: 420Kg/m^3

Cost: £49.99

 

Properties of the plastic toy: I am a plastic object. I am strong and hard wearing, I can be moulded and I am and electrical and heat insulator.

Density: 920Kg/m^3

Cost: £3.5

 

Properties of the saucepan: I am a metal object. I am strong, hard and shiny, and I am and good conductor of heat and electricity.

Density: 8000Kg/m^3

Cost: £7.5

 

Properties of the teacup: I am a ceramic object. I am hard wearing but brittle, heat resistant and I am an electrical and heat insulator.

Density: 2130Kg/m^3

Cost: £2.25

 

Properties of the discounted wooden table: I am a material. I am a solid and can be used in manufacturing.

Density: 420Kg/m^3

Cost: £47.99

 

Properties of the discounted plastic toy: I am a plastic object. I am strong and hard wearing, I can be moulded and I am and electrical and heat insulator.

Density: 920Kg/m^3

Cost: £1.5

 

 

Process returned 0 (0x0)   execution time : 0.007 s

Press ENTER to continue.

*/

 

/*

        Copyright © Robin Broad 2018

        This program is free software: you can redistribute it and/or modify

        it under the terms of the GNU General Public License as published by

        the Free Software Foundation, either version 3 of the License, or

        (at your option) any later version.

 

        This program is distributed in the hope that it will be useful,

        but WITHOUT ANY WARRANTY; without even the implied warranty of

        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

        GNU General Public License for more details.

 

        GNU General Public License: https://www.gnu.org/licenses/gpl.html

*/