0% found this document useful (0 votes)
13 views25 pages

(OOP) Assignment

Uploaded by

hussoali12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

(OOP) Assignment

Uploaded by

hussoali12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Program # 01

#include < iostream >


using namespace std;
class Student
{
private:
int adminno;
string sname;
float math, eng, science, total;
float ctotal()
{
total = math + eng + science;
return total;
}
public:
void Takedata()
{
cout << "Enter the Number of Admin :";
cin >> adminno;
cout << "Enter the Name of Student :";
cin >> sname;
cout << "Enter the Number of Math :";
cin >> math;
cout << "Enter the Number of Science :";
cin >> science;
cout << "Enter the Number of English :";
cin >> eng;
ctotal ( );
}
void Showdata ( )
{
cout << "Admin No :" << adminno << '\n'<< "Student : " <<
sname << '\n'<< "Total Marks: " << total;
} };
int main ( )
{
Student s;
s.Takedata();
s.Showdata();
return 0;
}

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

#include < iostream >


using namespace std;
class Test
{
private:
int TestCode, NoCandidate;
float NoCenters;
string Descrip on;
float CalCNTR ( )
{
NoCenters = (NoCandidate / 100 + 1);
}
public:
void Schedule ( )
{
cout << "Enter the Test Code: ";
cin >> TestCode;
cout << "Enter the Descrip on: ";
cin >> Descrip on;
cout << "Number of Candidates: ";
cin >> NoCandidate;
CalCNTR ( );
}
void DispTest ( ) {
cout<<"Test Code: "<<TestCode<<endl<<"Descrip on :
"<<Descrip on<<endl<<"Number of Centers : "<<NoCenters;
} };
int main ( )
{
Test T;
T.Schedule();
T.DispTest();
return 0;
}

Program # 04

#include < iostream >


using namespace std;
class Jet
{
private:
int Flightno;
string Des na on;
float distance, fuel;
float Calfuel ( )
{
if (distance <= 1000)
fuel = 500;
else if (distance > 1000 && distance <= 2000)
fuel = 1100;
else
fuel = 2200;
}
public:
void FeedInfo ( )
{
cout << "Enter the Flight Number : ";
cin >> Flightno;
cout << "Enter the Des na on : ";
cin >> Des na on;
cout << "Enter the Total Distance :";
cin >> distance;
Calfuel ( );
}
void ShowInfo ( )
{
cout << "Flight Number : " << Flightno << endl <<
"Des na on : "<<Des na on << endl << "Distance : " <<
distance<<" Km" << endl<< "Fuel : " << fuel <<" liters";
} };
int main ( )
{
Jet j;
j.FeedInfo();
j.ShowInfo();
return 0;
}

Program # 05

#include < iostream >


using namespace std;
class Book
{
private:
int bookno;
string book tle;
float price;
float TotalCost (int c)
{
float Tprice = price * c;
cout<< Tprice;
}
public:
void Input ( );
void Purchase ( );
};
void Book : : Input( )
{
cout << " Book Number : ";
cin >> bookno;
cout << " Book Title : ";
cin >> book tle;
cout << " Price :";
cin >> price;
}
void Book : : Purchase ( )
{
int copies;
cout << "Enter the Number of Copies : ";
cin >> copies;
Input ( );
TotalCost (copies);
}
int main ( )
{
Book b;
b.Purchase( );
return 0;
}

Program # 06

#include < iostream >


using namespace std;
class Report
{
private:
int adminno, sub[5];
string name, subject[5];
float avg, marks;
float GetAvg (int s)
{
avg = s / 5.0;
}
public:
void ReadInfo ( )
{
cout << "Admin Number : ";
cin >> adminno;
cout << "Name : ";
cin >> name;
int s = 0;
for (int i = 0; i < 5; i++)
{
cout << "Enter the " << i + 1 << " Subject Marks : ";
cin >> sub[i];
s = s + sub[i];
}
GetAvg (s);
}
void DisplayInfo ( )
{
cout << "Admin no: " << adminno << '\n'<< "Name :" <<
name << '\n'<< "Average : " << avg;
} };
int main ( )
{
Report r;
r.ReadInfo( );
r.DisplayInfo( );
return 0;
}

Program # 07

#include < iostream >


using namespace std;
class Rectangle
{
private:
float length;
float width;
public:
Rectangle (float l = 0, float w = 0)
{
length = l;
width = w;
}
void setlength (float l)
{ length = l; }
void setwidth (float w)
{ width = w; }
float perimeter ( )
{ return 2 * (length + width); }
float area ( )
{ return length * width; }
void show()
{ cout << "Rectangle with length " << length << " and
width " << width << endl; }
int sameArea( Rectangle &rect2)
{ return area( ) == rect2.area( ); } };
int main ( )
{
Rectangle rect1(5, 6.8);
Rectangle rect2(5, 18.9);

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

#include < iostream >


using namespace std;
class complex
{
private:
float real;
float imag;

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

#include < iostream >


using namespace std;
class Distance
{
private:
int feet;
float inches;
public:
Distance ( )
{
feet = 0;
inches = 0;
}
Distance (int f, float i)
{
feet = f;
inches = i;
}
void Set (int, float);
void disp ( );

Distance add (Distance &y)


{
return Distance(feet + y.feet, inches + y.inches);
} };

void Distance : : Set(int f, float i)


{
feet = f;
inches = i;
}
void Distance : : disp ( )
{
cout << feet << " Feet" << " " << inches << " Inches" << endl;
}
int main ( )
{
Distance x, y, z;
x.Set (85, 98.3);
y.Set (54, 85.3);
z = x.add (y);
cout << "Distance 1 :";
x.disp( );
cout << "Distance 2 :";
y.disp( );
cout << "Distance 3 :";
z.disp( );
return 0;
}

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

#include < iostream >


#include< cstring >
using namespace std;
class Test
{
char paper [20];
int marks;
public:
Test ( ) // Func on 1
{
strcpy (paper, "Computer");
marks = 0;
}
Test (char p [] ) // Func on 2
{
strcpy (paper, p);
marks = 0;
}
Test (int m) // Func on 3
{
strcpy (paper ,"Computer");
marks = m;
}
Test (char p [], int m) // Func on 4
{
strcpy (paper, p);
marks = m;
}
};
int main ( ) {
char k['l'];
Test t1, t2(k), t3(5), t4(k,4);
return 0;
}

Program # 13

#include < iostream >


using namespace std;
class Sample
{
int x;
double y;
public:
Sample ( )
{
x = 0;
y = 0;
}
Sample (int x)
{ cout << x << endl; }
Sample (int x, int y)
{ cout << x << '\t' << y << endl; }
Sample (int x, double y)
{ cout << "This is 4" << endl << x << '\t' << y << endl; }
};

int main ( )
{
Sample s1, s2(25), s3(7, 52), s4(89, 5.3);
return 0;
}

Program # 14

#include < iostream >


using namespace std;
class cashRegister
{
private:
int cashOnHand;
public:
cashRegister ( )
{ cashOnHand = 500; }
cashRegister (int cash)
{ cashOnHand = cash; }
int getCurrentBalance ( )
{ return cashOnHand; }
void acceptAmount (int amount)
{ cashOnHand += amount; }
};

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;
}

int cost = product.getCost ( );


cout << "The cost of the selected item is: Rs." << cost << endl;
cout << "Please deposit the money: ";
int amount;
cin >> amount;

if (amount >= cost)


{
cashRegisterMachine.acceptAmount (cost);
product.makeSale( );
cout << "Thank you for your purchase! Enjoy your item." <<
endl;
}

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;
}

You might also like