You are on page 1of 14

Assignment-6

Submitted by-

Name-Arshvir Singh

Ques:1
Roll no.- 101808148
Soln:

#include<iostream> Group- F3

#include<math.h>

using namespace std;

class triangle

private :

int l,h;

float a;

public :

void area(int x)//Area of equilateral triangle//

a=((pow(3,0.5)*x*x)/4);

cout<<"Area = "<<a;

void area(int y,int z)//area of right angled triangle//

a=(0.5*y*z);

cout<<"Area = "<<a;

void area(int q,int w,int e)//Area of isoceles triangle//


{

int r=pow((w*w-((e*e)/4)),0.5);

a=0.5*r*e;

cout<<"Area = "<<a;

void area(float y1,float a1,float b1,float c1)//Area od scalene triangle//

a=pow((y1*(y1-a1)*(y1-b1)*(y1-c1)),0.5);

cout<<"Area = "<<a;

};

int main()

triangle f;

int n,m,j,k,p,o,u;

float a2,b2,c2,d2;

char r;

//Makiing of table for choices//

cout<<endl<<"Enter the type of triangle :"<<endl<<"--------------------------------------


"<<endl<<"1.Equilateral Triangle"<<endl<<"2.Right Angled Triangle"<<endl<<"3.Isosceles
Triangle"<<endl<<"4.Scalene Triangle"<<endl;

cout<<endl<<"Enter Your choice :";

cin>>n;

switch(n)//Switch case for selection of option//

case 1:

cout<<endl<<"Enter side of your equilateral triangle = ";

cin>>m;
f.area(m);//Area function with one argument is called//

break;

case 2:

cout<<endl<<"Enter hieght and base length of your right angled triangle = ";

cin>>j>>k;

f.area(j,k);/ /Area function with two argument is called//

break;

case 3:

cout<<endl<<"Enter equal side of your isosceles triangle = ";

cin>>o;

p=o;

cout<<"Enter base length of your triangle = ";

cin>>u;

f.area(o,p,u); / /Area function with three argument is called//

break;

case 4:

cout<<endl<<"Enter first side of triangle = ";

cin>>b2;

cout<<endl<<"Enter second side of triangle = ";

cin>>c2;

cout<<endl<<"Enter third side of triangle = ";

cin>>d2;

a2=(b2+c2+d2)/2;

f.area(a2,b2,c2,d2); / /Area function with four argument is called//

cout<<endl<<endl<<"Do you want to run this program again ? (y/n) : ";

cin>>r;
//Re-running of program//

switch(r)

{ case 'y':

main();

break;

case 'n':

return 0;

return 0;}

Output:

Ques:2

Soln:

#include<iostream>

using namespace std;

class publish

{
public:

char n[30];

int p;

virtual void get_data()//virtual function to get details//

cout<<"Enter Name of Book/Cassette : ";

cin.get(n,30);

cout<<"Enter Price : ";

cin>>p;

virtual void put_data()//Virtual function to display details//

cout<<"Name : "<<n<<endl<<"Price : "<<p<<endl;

};

class book:public publish//class publish inherited in class book//

private:

int page;

public:

void get_data()//Overriding of virtual function//

cout<<"Enter number of pages : ";

cin>>page;

void put_data()//Function overriding//

{
cout<<"Pages : "<<page<<endl;

};

class tape:public publish//class publish inherited in class tape//

private :

int play;

public :

void get_data()//Overriding of virtual function//

cout<<"Enter Playing Time of tape in minutes ";

cin>>play;

void put_data()//Function overriding//

cout<<"Play-time : "<<play<<" minutes"<<endl;

};

int main()

publish *p[2];//Decleration of pointer of base class//

p[0]=new publish;//Dyanmic memory allocation//

p[0]->get_data(); //Get data function of class publish called//

p[1]=new book;//Dynamic memory allocation//

p[1]->get_data();//get data function of class book called//

p[0]->put_data();//put data function of class publish called//

p[1]->put_data();//put data function of class book called//


cout<<"--------------------------------"<<endl;

p[2]=new tape;//Dyamic memory allocation for object of class tape//

p[2]->get_data();//get data function of class tape called//

p[0]->put_data();

p[2]->put_data();//put data pf class tape called//

return 0;

Output:

Ques:3

Soln:

#include<iostream>

using namespace std;

void display(char c='*',int n=1)//Decleration of display function//

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

cout<<" "<<c;

cout<<endl;

int main()

char a;

int b;

int n;

char r;

cout<<"Enter Number of arguments you want to enter : \n1.No argument \n2.1 Argument \n3.2
Arguments \nEnter Your choice : ";

cin>>n;

switch(n)//Switch case as per number of arguements//

case 1:

display();

break;

case 2:

cout<<"Enter any symbol : ";

cin>>a;

display(a);

break;

case 3:

cout<<"Enter any symbol : ";

cin>>a;

cout<<"Enter no of times you want to print this symbol : ";

cin>>b;
display(a,b);

break;

cout<<"Do you want to run the program again?(y/n) :";

cin>>r;

switch(r)//Switch case to re-run the program//

case 'y':

main();

break;

case 'n':

return 0;

break;

return 0;

Output:
Ques:4

Soln:

#include<iostream>

using namespace std;

class Complex{

private:

float r;

float i;

public:

Complex()//Constructor//

cout<<"Enter the real and imaginary part of complex number: ";

cin>>r>>i;

Complex(float x,float y)//Constructor overloaded//

r=x;

i=y;

void display()

cout<<r<<" + "<<"i"<<i;

Complex sum(Complex &c)//Copy constructor//

{
Complex s(0.0,0.0);

s.r=this->r+c.r;//USe of this pointer which is pointing to values of r and i of first complex//

s.i=this->i+c.i;

return s;

};

int main()

Complex c1,c2,c3(0.0,0.0);

cout<<"First complex number=";

c1.display();

cout<<endl;

cout<<"Second complex number=";

c2.display();

cout<<endl;

c3=c1.sum(c2);

cout<<endl<<"Sum = ";

c3.display();

return 0;

Output:
Ques:5

Soln:

#include<iostream>

using namespace std;

class A

private :

int a;

int b;

public :

A()//Constructor//

{
a=10;

b=15;

A(int x)//Constructor with one argument-constructor overloding//

a=x;

b=15;

A(int y,int z)//Constructor with two arguments-constructor overloding//

a=y;

b=z;

A(A &p)//Copy constructor//

a=p.a*5;

b=p.b*5;

void display()

cout<<"a = "<<a<<" b = "<<b;

};

int main()

A o1;

cout<<"After no argument constructor values are ";


o1.display();

A o2(5);

cout<<endl<<"After one argument constructor values are ";

o2.display();

A o3(20,25);

cout<<endl<<"After two argument constructor values are ";

o3.display();

A o4(o3);

cout<<endl<<"After Copy constructor ,values are ";

o4.display();

return 0;

Output:

You might also like