(OOP) Assignment
(OOP) Assignment
(OOP) Assignment
Program #02
#include < iostream >
using namespace std;
class Batsman
{
private:
int bcode, innings, notout, runs;
string bname;
float avg;
float calcavg ( )
{
avg = runs / (innings - notout);
}
public:
void Readdata ( )
{
cout << "Enter the Batsman Code :";
cin >> bcode;
cout << "Enter the Name of Batsman :";
cin >> bname;
cout << "Enter the Number of Innings :";
cin >> innings;
cout << "Enter the Number of Not Outs :";
cin >> notout;
cout << "Enter the Runs of Batsman :";
cin >> runs;
calcavg ( );
}
void Displaydata ( )
{
cout << "Id: " << bcode << '\n'<< "Name :" << bname <<
'\n'<< "Average :" << avg << endl;
} };
int main ( )
{
Batsman B;
B.Readdata();
B.Displaydata();
return 0;
}
Program # 03
Program # 04
Program # 05
Program # 06
Program # 07
rect1.show( );
cout << "Area: " << rect1.area( ) << endl;
cout << "Perimeter: " << rect1.perimeter( ) << endl;
rect2.show( );
cout << "Area: " << rect2.area( ) << endl;
cout << "Perimeter: " << rect2.perimeter( ) << endl;
if (rect1.sameArea(rect2))
cout << "The two rectangles have the same area." << endl;
else
cout << "The two rectangles don't have the same area." <<
endl;
rect1.setlength (15);
rect1.setwidth (6.3);
rect1.show( );
cout << "Area: " << rect1.area() << endl;
cout << "Perimeter: " << rect1.perimeter() << endl;
rect2.show( );
cout << "Area: " << rect2.area() << endl;
cout << "Perimeter: " << rect2.perimeter() << endl;
if (rect1.sameArea(rect2))
cout << "The two rectangles have the same area." <<
endl;
else
cout << "The two rectangles don't have the same area."
<< endl;
return 0;
}
Program # 08
public:
complex ( )
{
real = 0;
imag = 0;
}
complex (float r, float i)
{
real = r;
imag = i;
}
void set (float r, float i)
{
real = r;
imag = i;
}
void disp ( )
{ cout << real << " + " << imag << "i" << endl; }
complex sum(complex &c2)
{
return complex(real + c2.real, imag + c2.imag);
} };
int main ( )
{
complex c1, c2, c3;
c1.set (3.5, 2.5);
c2.set (1.5, 4.5);
c3 = c1.sum(c2);
cout << "Complex number 1: ";
c1.disp ( );
cout << "Complex number 2: ";
c2.disp ( );
cout << "Sum of complex numbers: ";
c3.disp ( );
return 0;
}
Program # 09
Program # 10
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time ( )
{
hours = 0;
minutes = 0;
}
Time (int f, int i)
{
hours = f;
minutes = i;
}
void SetTime (int, int);
void ShowTime ( );
Time sum ( Time &y)
{
return Time(hours + y.hours, minutes + y.minutes);
} };
void Time : : SetTime (int f, int i)
{
hours = f;
minutes = i;
}
void Time : : ShowTime ( )
{
cout << hours << " Hours"<< " " << minutes << " Minutes" <<
endl;
}
int main ( )
{
Time x, y, z;
x.SetTime (1, 6);
y.SetTime (85, 23);
z = x.sum(y);
cout << "First Time :";
x.ShowTime ( );
cout << "Second Time :";
y.ShowTime ( );
cout << "Sum of Time :";
z.ShowTime ( );
return 0;
}
Program # 11
#include <iostream>
using namespace std;
class Seminar
{
int me;
public:
Seminar ( ) //Func on 1
{
me = 30;
cout << "Seminar starts now" << endl;
}
void lecture ( ) //Func on 2
{
cout << "Lectures in the seminar on" << endl;
}
Seminar (int dura on) //Func on 3
{
me = dura on;
cout << "Seminar starts now 3" << endl;
}
~ Seminar ( ) //Func on 4
{
cout << "Thanks" << endl;
} };
int main ( ) {
Seminar s1, s(5); // To Ac vate Func on 1 & 3 and it also
ac vates the DeConstructor Func on 4
return 0;
}
Program # 12
Program # 13
int main ( )
{
Sample s1, s2(25), s3(7, 52), s4(89, 5.3);
return 0;
}
Program # 14
class dispenserType
{
private:
int numberOfItems;
int cost;
public:
dispenserType ( )
{
numberOfItems = 50;
cost = 50;
}
dispenserType (int items, int itemCost)
{
numberOfItems = items;
cost = itemCost;
}
int getNoOfItems ( )
{
return numberOfItems;
}
int getCost ( )
{
return cost;
}
void makeSale ( )
{
numberOfItems - - ;
} };
void showSelec on ( )
{
cout << "Welcome to the candy machine!" << endl;
cout << "Products available: " << endl;
cout << "1. Candies" << endl;
cout << "2. Chips" << endl;
cout << "3. Gum" << endl;
cout << "4. Cookies" << endl;
cout << "Please select the product you want to buy (1-4): ";
}
void sellProduct (dispenserType &product, cashRegister
&cashRegisterMachine)
{
int choice;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You selected Candies." << endl;
break;
case 2:
cout << "You selected Chips." << endl;
break;
case 3:
cout << "You selected Gum." << endl;
break;
case 4:
cout << "You selected Cookies." << endl;
break;
default:
cout << "Invalid choice." << endl;
return;
}
else
cout << "Insuffi cient amount deposited. Transac on
canceled." << endl;
}
int main ( )
{
cashRegister cashRegisterMachine;
dispenserType candies, chips, gum, cookies;
showSelec on ( );
sellProduct (candies, cashRegisterMachine);
return 0;
}