Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

INTERNATIONAL ISLAMIC UNIVERSITY ISLAMABAD

DEPARTMENT OF ELECTRICAL ENGINEERING


OBJECT OREINTED PROGRAMMING
ASSIGNMENT # 01
SUBMITTED BY: GROUP 7
SHEHWAR TANVEER 860-BSEE/FET/F20
MAHNOOR ABDUL MALIK 859-BSEE/FET/F20
AZZA ABEER 849-BSEE/FET/F20
SUBMITTED TO: MAM IRUM NOSHEEN
QUESTION # 01
#include <iostream>

#include <stdlib.h>

using namespace std;

class Circle

private:

float x ; float y ; float r ; float Pi; //DATA MEMBERS//

public:

Circle () : x (0.0), y (0.0) , r (0.0), Pi(3.141593F) //FUNCTION MEMBERS//

{}
Circle( float X, float Y, float R) : x (X), y(Y) , r(R), Pi(3.141593F)

{}

void set_values(){

cout << "Enter Value Of X- Coordinate: "; cin >> x;

cout << "Enter Value Of Y- Coordinate: "; cin >> y;

cout << "Enter Radius Of Circle "; cin >> r;

float area(){

return (Pi*r*r) ; //CALCULATE AREA OF CIRCLE//

float circumference() {

return (2*Pi*r); //CALCULATE CIRCUMFERENCE OF CIRCLE

}
void print(){

cout << "X-Coordinate = " << " "<<x <<endl;

cout << "Y-Coordinate = " <<" "<< y <<endl;

cout << "Radius = "<<" "<< r <<endl;

};

int main(){

Circle c1;

//CALLING FUNCTIONS IN MAIN TO TEST THIER WORKING//

Circle c2(9.0F, 14.0F, 8.0F);

c1.set_values();

c1.print();

c2.print();

cout<<"\n";

cout<<"**********************************"<<endl;

cout <<"*"<<" "<< "Area Of C 1 = " << c1.area()<<"\t"<<" "<<"*"<< endl;

cout <<"*"<<" "<< "Area Of C 2 = " << c2.area()<<"\t\t" <<" "<<"*"<<endl;


cout <<"*"<<" "<<"Circumference Of C 1 = " << c1.circumference()<<" "<<"*"<< endl;

cout <<"*"<<" "<< "Circumference Of C 2 = " << c2.circumference()<<" "<<"*" << endl;

cout<<"**********************************"<<endl;

system("PAUSE");

return 0;

QUESTION # 02

#include <iostream>

#include <stdlib.h>

using namespace std;

class rectangle

private:

int length,width;

public:

rectangle() : length(1),width(1){
}

rectangle(int l,int w) : length(l),width(w){

void set(int l,int w)

length = l;

width = w;

int perimeter()

int perimeter;

perimeter=2*length+2*width;

return perimeter;

int area()

int area;

area=length*width;

return area;

void get()

cout<<"Enter Length:";

cin>>length;

cout<<endl;

cout<<"Enter Width:";

cin>>width;
cout<<endl;

void draw()

int i,j;

for (i=1;i<=(width);i++)

cout<<"*";

cout<<endl;

for (j=1;j<=(length-2);j++)

cout<<"*";

for (i=0;i<(width-2);i++)

cout<<" ";

cout<<"*";

cout<<endl;

for (i=1;i<=(width);i++)

cout<<"*";
}

};

int main()

rectangle r1,r2(3,5);

r1.get();

cout<<"Area of r1: ";

cout<<r1.area();

cout<<endl;

cout<<"Perimeter of r1: ";

cout<<r1.perimeter();

cout<<endl;

cout<<"Rectangle of r1: "<<endl;

r1.draw();

cout<<endl;

//

cout<<"Area of r2: ";

cout<<r2.area();

cout<<endl;

cout<<"Perimeter of r2: ";

cout<<r2.perimeter();

cout<<endl;

cout<<"Rectangle of r2: "<<endl;

r2.draw();
cout<<endl;

system("PAUSE");

return 0;

QUESTION # 03
#include <iostream>

#include <stdlib.h>

#include <stdio.h>

using namespace std;

class Time

private:

int hrs;

int min;

int sec;

public:

Time()

hrs=0;

min=0;

sec=0;

void set(int,int,int);

void get();

void show();

void tick();
Time operator ++()

Time temp;

++sec;

if(sec>59)

sec -=60;min++;

if(min>59)

{min -=60;hrs++;

if (hrs>23)

hrs -=24;

temp.sec=sec;

temp.min=min;

temp.hrs=hrs;

return temp;

Time operator--()

Time zemp;

--sec;

if(sec<0)

sec+=60;min--;

}
if(min<0)

min +=60;hrs--;

if(hrs<0)

hrs+=24;

zemp.sec=sec;

zemp.min=min;

zemp.hrs=hrs;

return zemp;

Time operator ++(int)

Time cemp;

++sec;

if(sec>59)

sec -=60;min++;

if(min>59)

{min -=60;hrs++;

if (hrs>23)

hrs -=24;
}

cemp.sec=sec;

cemp.min=min;

cemp.hrs=hrs;

return cemp;

Time operator --(int)

Time lemp;

Time zemp;

sec--;

if(sec<0)

sec+=60;min--;

if(min<0)

min +=60;hrs--;

if(hrs<0)

hrs+=24;

lemp.sec=sec;

lemp.min=min;

lemp.hrs=hrs;

return lemp;

}
};

void Time::set(int h,int m,int s)

hrs=h;

min=m;

sec=s;

void Time::get()

cout<<"|| GET THE HOURS FROM USER ||"<<endl;

cout<<"\n";

cout<<"Enter The Hours"<<endl;

cin>>hrs;

cout<<"|| GET THE MINUTES FROM USER ||"<<endl;

cout<<"\n";

cout<<"Enter The Minutes"<<endl;

cin>>min;

cout<<"|| GET THE SECONDS FROM USER ||"<<endl;

cout<<"\n";

cout<<"Enter The Seconds"<<endl;

cin>>sec;

void Time::show()
{

cout<<"TIME = "<<endl;

if(hrs<10)

cout<<"0"<<hrs<<":"<<endl;

else

cout<<hrs<<":"<<endl;

if(min<10)

cout<<"0"<<min<<":"<<endl;

else

cout<<min<<":"<<endl;

if(sec<10)

cout<<"0"<<sec<<":"<<endl;

else

cout<<sec<<":"<<endl;

void Time ::tick()

sec++;

if(sec>59)

{
sec -=60;min++;

if(min>59)

{min -=60;hrs++;

if (hrs>23)

hrs -=24;

int main()

char Sleep;

Time t1;

t1.get();

for(int i=1;i<=1000;i++)

--t1;

t1.show();

Sleep=1000

return 0;

}
QUESTION # 04

#include <iostream>

using namespace std;

class rational

private:

int nr1,nr2,dr1,dr2;

int GCD,smallernum,LCM;

int NR,DR;

public:

rational()

nr1 = 1;dr1 = 1;

nr2 = 1;dr2 = 1;

cout<<"ENTER TWO RATIONAL NUMBERS :- "<<endl;

cout<<"Enter numerator of 1st rational number : ";cin>>nr1;

cout<<"Enter denominator of 1st rational number : ";cin>>dr1;

cout<<"Enter numerator of 2nd rational number : ";cin>>nr2;

cout<<"Enter denominator of 2nd rational number : ";cin>>dr2;

cout<<endl;

if(nr1<dr1){
smallernum=nr1;

else{

smallernum=dr1;

for(int i=1;i<=smallernum;i++)

if(nr1%i==0 && dr1%i==0)

GCD=i;

nr1 = nr1/GCD ;

dr1 = dr1/GCD ;

//first fraction has been stored in its reduced form.

if(nr2<dr2){

smallernum=nr2;

else{

smallernum=dr2;

}
for(int i=1;i<=smallernum;i++)

if(nr2%i==0 && dr2%i==0)

GCD=i;

nr2 = nr2/GCD ;

dr2 = dr2/GCD ;

//2nd fraction has been stored in its reduced form.

void rational_print()

cout<<"Two Rational Numbers (in fraction form) :-"<<endl;

cout<<"1st fraction (in its simplest form) : "<<nr1<<"/"<<dr1<<endl;

cout<<"2nd fraction (in its simplest form) : "<<nr2<<"/"<<dr2<<endl;

cout<<endl;

void float_print()

{ float f1,f2 ;

float n1,d1,n2,d2;
n1 = nr1; n2 = nr2; d1 = dr1; d2 =dr2;

f1 = n1/d1 ;

cout<<"Decimal format of the declared rational numbers :-"<<endl;

cout<<"1st rational number in decimal format : "<<f1<<endl;

f2 = n2/d2 ;

cout<<"2nd rational number in decimal format : "<<f2<<endl;

cout<<endl;

void rational_add()

cout<<endl;

cout<<"Sum of the declared rational numbers :- "<<endl;

if(dr1<dr2)

smallernum=dr1;

else

smallernum=dr2;

for(int i=1;i<=smallernum;i++)

if(dr1%i==0 && dr2%i==0)


{

GCD=i;

LCM=((dr1*dr2)/GCD);

DR=LCM;

NR= ((DR/dr1)*nr1 + (DR/dr2)*nr2);

cout<<"Original Form of fractional sum : "<<NR<<"/"<<DR<<endl;

if(NR<DR){

smallernum=NR;

else{

smallernum=DR;

for(int i=1;i<=smallernum;i++)

if(NR%i==0 && DR%i==0)

GCD=i;
}

NR = NR/GCD ;

DR = DR/GCD ;

cout<<"Reduced form of fractional sum : "<<NR<<"/"<<DR<<endl;

cout<<endl;

void rational_subtract()

cout<<"Result of Subtraction of declared rational numbers :- "<<endl;

if(dr1<dr2)

smallernum=dr1;

else

smallernum=dr2;

for(int i=1;i<=smallernum;i++)
{

if(dr1%i==0 && dr2%i==0)

GCD=i;

LCM=((dr1*dr2)/GCD);

int DR = LCM;

int NR = (((DR/dr1)*nr1) - ((DR/dr2)*nr2));

if (NR==0)

cout<<"Result of subtraction : 0 "<<endl<<endl;

else

cout<<"Fractional result of subtraction (actual form) : "<<NR<<"/"<<DR<<endl;

if(NR<DR){

smallernum=NR;

else{

smallernum=DR;

}
for(int i=1;i<=smallernum;i++)

if(NR%i==0 && DR%i==0)

GCD=i;

NR = NR/GCD ;

DR = DR/GCD ;

cout<<"Fractional result of subtraction (reduced form) : "<<NR<<"/"<<DR<<endl;

cout<<endl;

void rational_multiply()

int NR = nr1*nr2;

int DR = dr1*dr2;

if(NR<DR){

smallernum=NR;

else{
smallernum=DR;

for(int i=1;i<=smallernum;i++)

if(NR%i==0 && DR%i==0)

GCD=i;

NR = NR/GCD ;

DR = DR/GCD ;

cout<<"Fractional result of multiplication :"<<NR<<"/"<<DR<<endl;

cout<<endl;

void rational_divide()

NR = nr1*dr2;

DR = dr1*nr1;

if(NR<DR){

smallernum=NR;
}

else{

smallernum=DR;

for(int i=1;i<=smallernum;i++)

if(NR%i==0 && DR%i==0)

GCD=i;

NR = NR/GCD ;

DR = DR/GCD ;

cout<<"Fractional result of division :"<<NR<<"/"<<DR<<endl;

cout<<endl;

};

int main()

rational r1;
r1.rational_print();

r1.float_print();

r1.rational_add();

r1.rational_subtract();

r1.rational_multiply();

r1.rational_divide();

return 0;

QUESTION # 5

#include <iomanip>

#include <iostream>

#include <string>

using namespace std;

//

class lineType

private:

double a,b,c;

public:

lineType(){

a=b=c=1;

}
lineType(double x,double y,double z){

a=x;

b=y;

c=z;

double getA(){return a;}

double getB(){return b;}

double getC(){return c;}

void setA(double x){a=x;}

void setB(double y){b=y;}

void setC(double z){c=z;}

//slope

double slope(){

if (b==0)

cout << "Line is vertical" << endl;

else

return -a/b;

};

//perpendicular lines

bool perpendicular(lineType line)

if(a*line.a==-b*line.b)

return true;
}

else

return false;

//parallel lines

bool parallel(lineType line)

if (-a*line.b==-line.a*b)

return true;

else

return false;

//equal lines

bool equal(lineType line)

if ((a==line.a && b==line.b && c==line.c)||(a/line.a==b/line.b) && (b/line.b==c/line.c))

cout <<"equal lines" << endl;

return true;

else

{
return false;

void Intersection(lineType line)

double determinant = a*line.b - line.a*b;

if (determinant == 0)

cout<<" are parallel"<<endl;

else

double x = (line.b*c - b*line.c)/determinant;

double y = (a*line.c - line.a*c)/determinant;

cout<<" intersected at: "<<x<<","<<y<<endl;

};

//main

int main()

lineType line1(1,2,3);
lineType line2(3,0,5);

lineType line3(6,8,10);

//call slope

cout <<"Slope of line 1:"<< line1.slope()<< endl;

cout <<"Slope of line 2:"<< line2.slope() << endl;

cout <<"Slope of line 3:"<< line3.slope() << endl;

//call equal

cout <<"Is line 2 equal to 1: "<< (line2.equal(line1)?"Yes":"No") << endl;

cout <<"Is line 2 equal to 3: "<< (line2.equal(line3)?"Yes":"No") << endl;

//call parallel

cout <<"Is line 2 parallel to 1:"<<(line2.parallel(line1)?"Yes":"No")<< endl;;

cout <<"Is line 2 parallel to 3:"<<(line2.parallel(line3)?"Yes":"No")<< endl;;

//call perpendicular

cout <<"Is line 2 perpendicular to 1:"<<(line2.perpendicular(line1)?"Yes":"No")<< endl;;

cout <<"Is line 2 perpendicular to 3:"<<(line2.perpendicular(line3)?"Yes":"No")<< endl;;

//call line intersection

cout<<"Line 1 and 2 ";

line2.Intersection(line1);

cout<<endl;

cout<<"Line 2 and 3 ";

line3.Intersection(line2);

cout<<endl;

system("PAUSE");

return 0;

}
QUESTION# 6
#include<iostream>

#include<conio.h>

using namespace std;

class TicTacToe{

private:

char Box[9];

public:

TicTacToe(){

for (int i=1 ; i<10 ;i++)

Box[i] = i+0x30;

void Show();

void move(char Player);


int Winner();

void Play();

};

void TicTacToe::Show(){

char line1[] = {0xC9,0xCD,0xCD,0xCD,0xCB,0xCD,0xCD,0xCD,0xCB,0xCD,0xCD,0xCD,0xBB,0x00};

char line2[] = {0xCC,0xCD,0xCD,0xCD,0xCE,0xCD,0xCD,0xCD,0xCE,0xCD,0xCD,0xCD,0xB9,0x00};

char line3[] = {0xC8,0xCD,0xCD,0xCD,0xCA,0xCD,0xCD,0xCD,0xCA,0xCD,0xCD,0xCD,0xBC,0x00};

system("CLS");

cout<<"\n";

cout << "\nTic Tac Toe" << endl

<< "Player 1 (X) - Player 2 (O)" << endl << endl

<< line1 <<endl

<< '\xBA'<<' '<<Box[1]<<' '<<'\xBA'<<' '<<Box[2]<<' '<<'\xBA'<<' '<<Box[3]<<' '<<'\xBA'<<endl

<< line2 <<endl

<< '\xBA'<<' '<<Box[4]<<' '<<'\xBA'<<' '<<Box[5]<<' '<<'\xBA'<<' '<<Box[6]<<' '<<'\xBA'<<endl


<< line2 <<endl

<< '\xBA'<<' '<<Box[7]<<' '<<'\xBA'<<' '<<Box[8]<<' '<<'\xBA'<<' '<<Box[9]<<' '<<'\xBA'<<endl

<< line3 << endl;

void TicTacToe::move(char Player){

int i=0;

while(true){

cout<<"Enter Location (1-9): ";

cin >> i;

if( i < 1 && i >9 ){

cout<<"Location is incorrert." << endl;

continue;

}
if( Box[i]!='O' && Box[i]!='X'){

Box[i] = Player;

break;

else{

cout<<"This Location is not empty." << endl;

continue;

int TicTacToe::Winner(){

if (Box[1] == Box[2] && Box[2] == Box[3]) return 0; // Winner


else if (Box[4] == Box[5] && Box[5] == Box[6]) return 0; // Winner

else if (Box[7] == Box[8] && Box[8] == Box[9]) return 0; // Winner

else if (Box[1] == Box[4] && Box[4] == Box[7]) return 0; // Winner

else if (Box[2] == Box[5] && Box[5] == Box[8]) return 0; // Winner

else if (Box[3] == Box[6] && Box[6] == Box[9]) return 0; // Winner

else if (Box[1] == Box[5] && Box[5] == Box[9]) return 0; // Winner

else if (Box[3] == Box[5] && Box[5] == Box[7]) return 0; // Winner

else if ( Box[1] != '1' && Box[2] != '2' && Box[3] != '3' &&

Box[4] != '4' && Box[5] != '5' && Box[6] != '6' &&

Box[7] != '7' && Box[8] != '8' && Box[9] != '9'

)
return 1; // No One is Winner,

else

return 2; // Games is not Finished Yet

void TicTacToe::Play()

while(true){

Show();

cout <<" Player 1 (X) Moves Now" << endl;

move('X');

Show();

if( 0 == Winner()){

cout <<" Player 1 (X) Wins" << endl;

break;

}
if( 1 == Winner()){

cout <<" No One is Winner" << endl;

break;

cout <<" Player 2 (O) Move Now" << endl;

move('O');

if( 0 == Winner()){

Show();

cout <<" Player 2 (0) Wins" << endl;

break;

if( 1 == Winner()){

Show();

cout <<" No One is Winner" << endl;


break;

int main(){

TicTacToe t1;

t1.Play();

system ("PAUSE");

return 0;

You might also like