Professional Documents
Culture Documents
RAPORT
Lucrare de laborator Nr.5
la Limbajul de programare c++
Chişinău 2015
0
Laboratory work No.5
Variant 3
#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);
}
};
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
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.
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()
3
obj.base2::same_function( ); /* Function of class base2 is called. */
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;
# 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;
# 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;
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;
# 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;
# 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;
# 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;
7
Result for problem A:
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>
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>
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