You are on page 1of 12

Ministerul Educaţiei al Republicii Moldova

Universitatea Tehnică a Moldovei

Catedra: Filiera Anglofona

RAPORT
Lucrare de laborator Nr.5
la Limbajul de programare c++

A efectuat: st. gr. FAF-141


Botnari Nicolae

A verificat: dr., conf.univ.


M. Kulev

Chişinău 2015

0
Laboratory work No.5

Theme: Multiple inheritance


Aim of the study:
• Studying the rules for determining multiple inheritance;
• Studying multiple inheritance advantages and shortcomings;
• Problems using multiple inheritance;
• Solving problems;

Variant 3

Multiple Inheritance in C++


In C++ programming, a class can be derived from more than one parents. For example: A
classRectangle is derived from base classes Area and Circle.

Source Code to Implement Multiple Inheritance in C++


Programming
This program calculates the area and perimeter of an rectangle but, to perform this
program, multiple inheritance is used.

#include <iostream>
using namespace std;
class Area
{
public:
float area_calc(float l,float b)
{
return l*b;
}
};

class Perimeter

1
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};

/* Rectangle class is derived from classes Area and Perimeter. */


class Rectangle : private Area, private Perimeter
{
private:
float length, breadth;
public:
Rectangle() : length(0.0), breadth(0.0) { }
void get_data( )
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}

float area_calc()
{
/* Calls area_calc() of class Area and returns it. */

return Area::area_calc(length,breadth);
}

float peri_calc()
{
/* Calls peri_calc() function of class Perimeter and returns it. */

return Perimeter::peri_calc(length,breadth);
}
};

int main()
{
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"\nPerimeter = "<<r.peri_calc();
return 0;
}
Output

Enter length: 5.1

Enter breadth: 2.3

2
Area = 11.73

Perimeter = 14.8

Note: This program is intended to give you idea on how multiple inheritance works
rather than the condition in which multiple inheritance is used.

Ambiguity in Multiple Inheritance


Multiple inheritance may be helpful in certain cases but, sometimes odd sort of problem
encounters while using multiple inheritance. For example: Two base classes have
functions with same name which is not overridden in derived class and if you write code
to access that function using object of derived class, compiler shows error because, it
cannot determine which function to call. Here is a code for this type of ambiguity in
multiple inheritance

class base1
{
public:
void some_function( )
{ .... ... .... }
};
class base2
{
void some_function( )
{ .... ... .... }
};
class derived : public base1, public base2
{

};

int main()
{
derived obj;

/* Error because compiler can't figure out which function to call either same_function(
) of base1 or base2 .*/
obj.same_function( )
}

But, this problem can be solved easily using scope resolution function to specify which
function to class either base1 or base2

int main()

obj.base1::same_function( ); /* Function of class base1 is called. */

3
obj.base2::same_function( ); /* Function of class base2 is called. */

1. Analysis of data and functions from first variant


Class TV:
 diagonal – simple variable of integer type, the diagonal of the TV;

Class DigitalDevice:
 price – simple variable of integer type, the price of the digital device.

Class Monitor:
 rez– simple variable of integer type, the rezolution of the Monitor.

2.The functions:
Functions for first problem:
Functions for class TV

# Default Constructor
TV();
Returning type: void;
Parameters:
Void

# Constructor
TV(int diagonal) ;
Returning type: void;
Parameters:
diagonal – simple variable of integer type, the diagonal of the TV;

# Prints the TV diagonal


void printTV ();
Returning type: void;
Parameters:
void

Functions for class DigitalDevice

# Default Constructor
DigitalDevice ();
Returning type: void;
Parameters:
Void

# Constructor
DigitalDevice (int price) ;

4
Returning type: void;
Parameters:
price – simple variable of integer type, the pice of the Electrical device;

# Prints the Electrical Device price


void printDevice ();
Returning type: void;
Parameters:
void

Functions for class Monitor

# Default Constructor
Monitor ();
Returning type: void;
Parameters:
Void

# Constructor
Monitor (int price) ;
Returning type: void;
Parameters:
rez – simple variable of integer type, the rezolution of the Monitor;

# Prints the Monitor rezolution


void printMonitor ();
Returning type: void;
Parameters:
void

2. Analysis of data and functions from second variant


Class EDevice:
 guarantee– simple variable of integer type, the guarantee of the device;

Class TV:
 diagonal – simple variable of integer type, the diagonal of the TV;

Class DigitalDevice:
 price – simple variable of integer type, the price of the digital device.

Class Monitor:
 rez– simple variable of integer type, the rezolution of the Monitor.

5
Functions for the second problem:
Functions for class EDevice

# Default Constructor
EDevice ();
Returning type: void;
Parameters:
Void

# Constructor
EDevice (int diagonal) ;
Returning type: void;
Parameters:
guarantee – simple variable of integer type, the guarantee of the EDevice;

# Prints the EDevice guarantee


void printED();
Returning type: void;
Parameters:
void

Functions for class TV

# Default Constructor
TV();
Returning type: void;
Parameters:
Void

# Constructor
TV(int diagonal) ;
Returning type: void;
Parameters:
diagonal – simple variable of integer type, the diagonal of the TV;

# Prints the TV diagonal


void printTV ();
Returning type: void;
Parameters:
void

Functions for class DigitalDevice

# Default Constructor
DigitalDevice ();

6
Returning type: void;
Parameters:
Void
# Default Constructor
DigitalDevice ();
Returning type: void;
Parameters:
Void

# Constructor
DigitalDevice (int price) ;
Returning type: void;
Parameters:
price – simple variable of integer type, the pice of the Electrical device;

# Prints the Electrical Device price


void printDevice ();
Returning type: void;
Parameters:
void

Functions for class Monitor

# Default Constructor
Monitor ();
Returning type: void;
Parameters:
Void

# Destructor
~Monitor ();
Returning type: void;
Parameters:
Void

# Constructor
Monitor(const char* m,int d,int p,int x);
Returning type: void;
Parameters:
m – simple variable of pointer to char type, the name of the brand of the Monitor;

# Prints the Monitor rezolution


void printM();
Returning type: void;
Parameters:
void

7
Result for problem A:

Result for problem b:

Conclusion and verification :

Multiple inheritance in C++ is a powerful, but tricky tool, that often leads to problems if not used
carefully. This article will teach you how to use virtual inheritance to solve some of these
common problems programmers run into. If you're not familiar with multiple inheritance, check
out this article on multiple inheritance.

Bybliografy:
1. http://www.cplusplus.com/doc/tutorial/
2. https://en.wikipedia.org/wiki/C%2B%2B
3. http://www.tutorialspoint.com/cplusplus/

8
Appendix A:
#include <cstring>
# include<iostream>
# include<conio.h>

using namespace std;

class TV{
public:
int diagonal;
TV(){diagonal=0;}
TV(int diagonal){this->diagonal=diagonal;}
void printTV(){cout <<"Diagonal = "<<diagonal<<"\n"; }
};
class DigitalDevice{
public:
int price;
DigitalDevice(){price=0;}
DigitalDevice(int price){this->price=price;}
void printDevice(){cout <<"Price = "<<price<<"\n"; }
};
class Monitor: public TV, public DigitalDevice{
public:
int rez;
Monitor(){price=0;}
Monitor(int v,int d,int p):TV(d),DigitalDevice(p){this->rez=rez;}
void printM(){cout <<"Resolution = "<<rez<<" per inchi \n"; }
};

int main()
{
TV t(34);
DigitalDevice d(230000);
Monitor m(420,32,344500);
cout <<"Monitor exemple : \n";
m.printM();
m.printTV();
m.printDevice();
getch;
return 0;
}

9
Appendix B:
# include <cstring>
# include<iostream>
# include<conio.h>

using namespace std;


class EDevice{
public:
int guarantee;
EDevice(){guarantee=0;}
EDevice(int guarantee){this->guarantee=guarantee;}
void printED(){cout <<"Guarantee = "<< guarantee<<" years \n"; }
void inser(int g){guarantee=g;}
};

class TV: public virtual EDevice{


public:
int diagonal;
TV(){diagonal=0;}
TV(int diagonal, int x): EDevice(x){this->diagonal=diagonal;}
void printTV(){cout <<"Diagonal = "<<diagonal<<"\n"; }
void inser(int d){diagonal=d;}
};
class DigitalDevice:public virtual EDevice{
public:
int price;
DigitalDevice(){price=0;}
DigitalDevice(int price, int x): EDevice(x){this->price=price;}
void printDD(){cout <<"Price = "<<price<<" lei \n"; }
void inser(int p){price=p;}
};

class Monitor: public TV, public DigitalDevice{


public:
char* model;
Monitor(){model=NULL;}
Monitor(const char* m,int d,int p,int x): EDevice(d),TV(d,p),DigitalDevice(x,d){
this->model = new char[strlen(m)+1];
strcpy(this->model,m);
}
Monitor(const Monitor& b){
model = new char[strlen(b.model)+1];
strcpy(model, b.model);
}
~Monitor(){
if(this->model){delete[] this->model;}
}
void printM(){cout <<"Model = "<< this->model<<" \n"; }
};

10
int main()
{
cout <<"Monitor exemple : \n";
Monitor m("Samsung",10,42,20000);
m.printM();
m.printTV();
m.printDD();
m.printED();

getch;
return 0;
}

11

You might also like