assignment solved

50
Mail your solutions to [email protected] by 31 st October. Use the following pattern for subject of the mail. HIMANSHU GOEL || 9909103424 || OOPS ASSIGNMENT Question 1: There are several publishers located at different cities in India. Each publication house has some star authors, some known authors and some local/unknown authors. Authors are experts of different areas. One author may be an expert of maximum 3 areas. Authors responsibility is to write books in their area. Books will be of different pages, and written in different years. Books price may vary. Publication house maintains the records of no of copies sold in a year. Identify the classes and their relationships. Write a program to implement your classes and identified relationships. Further your program should display, Name of Book and the Author of the book having highest sale in a year. Question 2: Write a class in C++ representing a one dimensional Array of integer. Design suitable member functions to add, delete a value from array, sort array using insertion sort and search a value in the array using binary search. Operations should invoke at runtime based on user choice. Write a menu driven main to demonstrate all functions of an array class. Write at least three inputs with which you have tested your class. Question 3:- Consider the following code: #include<iostream.h> #include<conio.h> #include<dos.h> void pos(int,int); class stg

Upload: himanshu-goel

Post on 22-Nov-2014

131 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Assignment Solved

Mail your solutions to [email protected] by 31st October. Use the following pattern for subject of the mail.

HIMANSHU GOEL || 9909103424 || OOPS ASSIGNMENT

Question 1: There are several publishers located at different cities in India. Each publication house has some star authors, some known authors and some local/unknown authors. Authors are experts of different areas. One author may be an expert of maximum 3 areas. Authors responsibility is to write books in their area. Books will be of different pages, and written in different years. Books price may vary. Publication house maintains the records of no of copies sold in a year.

Identify the classes and their relationships. Write a program to implement your classes and identified relationships. Further your program should display, Name of Book and the Author of the book having highest sale in a year.

Question 2: Write a class in C++ representing a one dimensional Array of integer. Design suitable member functions to add, delete a value from array, sort array using insertion sort and search a value in the array using binary search. Operations should invoke at runtime based on user choice. Write a menu driven main to demonstrate all functions of an array class. Write at least three inputs with which you have tested your class.

Question 3:- Consider the following code:

#include<iostream.h>#include<conio.h>#include<dos.h>void pos(int,int);class stg{private:

char *p;public:

stg(char *temp){

p=temp;disp;

}

Page 2: Assignment Solved

void disp(){

pos(10,30);cout<<p;

}};void main(){

stg s1="ab cd";stg s2="fgbg";getch();

}void pos(int row,int col){

union REGS i;i.h.ah=0x02;i.h.bh=1;i.h.dh=row;i.h.dl=col;int86(0x10,&i,&i);

}

(a) Correct an error (if any) (b) Explain the output:

(i) On changing values of i.h.bh and i.h.ah(ii) On multiple execution after changing these values.

Ans.

Correct prog.

#include<iostream.h>#include<conio.h>#include<dos.h>void pos(int,int);class stg{private:

char *p;public:

stg(char *temp){

p=temp;

Page 3: Assignment Solved

disp(); // it was earlier error}void disp(){

pos(10,30);cout<<p;

}};void main(){

stg s1="himanshu";stg s2="goel";getch();

}void pos(int row,int col){

union REGS i;i.h.ah=0x02;i.h.bh=1;i.h.dh=row;i.h.dl=col;int86(0x10,&i,&i);

}

(b)

(i) no effect of value like 0x02,0x03,0x07 or any that I can try

(ii) himanshugoel himanshugoel himanshugoel

This type is not face in dev c++ because there is no function like clrscr() every time program execute there exist a fresh screen, thus don’t lead to multiple execution .

Question 4: Consider the following code

#include<iostream.h>#include<conio.h>#include<string.h>class test{

Page 4: Assignment Solved

char e_name[20];int e_age;public:

test(){

disp();cout<<endl<<"test constructor";

}disp(){

test1();cout<<endl<<"display function";

}test1(){

cout<<endl<<"test1 function";}test(char *name,int age){ strcpy(e_name,name); e_age=age;}test1(char *name,int age){

strcpy(e_name,name);e_age=age;

}~test(){

cout<<"name is:"<<e_name<<endl;cout<<"age is:"<<e_age<<endl;cout<<endl<<"reached destructor";

}};void main(){

clrscr();test t;test *p;p= new test;p->test1("oops",24);getch();

Page 5: Assignment Solved

}

Modify the above code so that it prints following output:Output:

Test1 functionDisplay functionTest constructorTest1 functionDisplay functionTest constructorName is :oopsAGE IS: 24REACHED DESTRUCTOR

Ans.#include<iostream.h>#include<conio.h>#include<string.h>class test{

char e_name[20];int e_age;public:

test(){

disp();cout<<endl<<"test constructor";

}disp(){

test1();cout<<endl<<"display function";

}test1(){

cout<<endl<<"test1 function";}test(char *name,int age){ strcpy(e_name,name); e_age=age;}test1(char *name,int age)

Page 6: Assignment Solved

{strcpy(e_name,name);e_age=age;

}~test(){

cout<<"name is:"<<e_name<<endl;cout<<"age is:"<<e_age<<endl;cout<<endl<<"reached destructor";

}};void main(){

clrscr();test t;test *p;p= new test;p->test1("oops",24);delete p; //the only addition to the codegetch();

}

Question 5: Each of the following programs has some errors .Locate them and record them in your notebook and correct if possible.

A

#include<iostream.h> class sample{private: static int count;public: sample() { count=10; }void main(){ sample s1,s2,s3;

Page 7: Assignment Solved

s1.display();s2.display();s3.display();} Ans.No declaration of display function in class.Correct program#include<iostream>

using namespace std; class sample{private: static int count;public: sample() { count=10; } static void display() {

cout<<count; } };int sample :: count;int main(){ sample s1,s2,s3;s1.display();s2.display();s3.display();getchar();return 0;}Prog. Output: 101010

Bint main() { float n1,n2,n3;

Page 8: Assignment Solved

cout<<” enter a number”; cin<<n1; cout<<”enter another number”; cin<<n2; n1+n2=sum; cout “the sum of the two number is “<< sum getchar(); return 0;}

Ans.Correct prog.

#include<iostream>#include<conio.h>

using namespace std;

int main(){ float n1,n2,n3; float sum; cout<<" enter a number"; cin>>n1; cout<<"enter another number"; cin>>n2; sum=n1+n2; cout <<"the sum of the two number is "<< sum; getchar(); return 0;} C.#include<iostream.h> int main(){ float num1,num2; cout<<”enter two number and I will multiply\n” cout<<”them by 50 for you.\n” cin>>num1>>num2; num=* 50; num =* 50; cout<< num1<<” “ <<num2;

Page 9: Assignment Solved

getchar(); return 0;}Ans.Correct prog.#include<iostream>#include<conio.h>

using namespace std;

int main(){ float num1,num2; cout<<"enter two number and I will multiply\n"; cout<<"them by 50 for you.\n"; cin>>num1>>num2; num1*= 50; num2*= 50; cout<< num1<<" "<<num2; getchar(); return 0;}

Question 6: What will the following program display on the screen record the output? If there are errors correct them and execute.

A……………………………………………..#include<iostream>

using namespace std;class student_rec{ private: int m1,m2,m3; float percentage; public: student_rec() { m1=m2=m3=0; percentage=0.0; }

Page 10: Assignment Solved

void calc_per(int x,int y,int z) { m1=x; m2=y; m3=z; percentage=(m1+m2+m3)/3.0; display_rec(); } void display_rec() { cout<<endl<<” percentage=”<< percentage<<”%”; }};

Ans. Correct program

#include<iostream>

using namespace std;class student_rec{ private: int m1,m2,m3; float percentage; public: student_rec() { m1=m2=m3=0; percentage=0.0; } void calc_per(int x,int y,int z) { m1=x; m2=y; m3=z; percentage=(m1+m2+m3)/3.0; display_rec(); } void display_rec() { cout<<endl<<" percentage="<< percentage<<"%"; }

Page 11: Assignment Solved

};

int main(){ student_rec s; s.calc_per(92,87,65); getchar(); return 0;}

Prog. Output: Percentage= 81.33333% B……………………………………………..#include<iostream>

using namespace std;

class tank{ private: int gallon; public: tank(void) { gallon = 50; } tank(int gal) { gallon = gal; } int getgallons(void) { return gallon; }};int main(){ tank storage[3]= {10,20}; for (int index=0; index<3;index++) cout<<storage[index].getgllons() <<endl; getchar(); return 0;

Page 12: Assignment Solved

}Ans. Correct prog.

#include<iostream>

using namespace std;

class tank{ private: int gallon; public: tank(void) { gallon = 50; } tank(int gal) { gallon = gal; } int getgallons(void) { return gallon; }};int main(){ tank storage[3]= {10,20}; for (int index=0; index<3;index++) cout<<storage[index].getgallons() <<endl; getchar(); return 0;}

Prog. Output: 10 20 50

C……………………………………………..#include<iostream> using namespace std;

Page 13: Assignment Solved

class package{private: int value;public: package(void) { value=7;cout<< value << endl; } package(int v) { value=v;cout<< value << endl; } ~package(void){ cout<< value << endl; }};int main(){ package obj1(4); package obj2(); package obj3(2); getchar(); return 0;}

Ans. Correct prog.

#include<iostream> using namespace std;

class package{private: int value;public: package(void) { value=7;cout<< value << endl;

Page 14: Assignment Solved

} package(int v) { value=v;cout<< value << endl; } ~package(void){ cout<< value << endl; }};int main(){ package obj1(4); package obj2(); package obj3(2); getchar(); return 0;}

#include<iostream> using namespace std;

class package{private: int value;public: package(void) { value=7;cout<< value << endl; } package(int v) { value=v;cout<< value << endl; } ~package(void){ cout<< value << endl; }};int main(){

Page 15: Assignment Solved

package obj1(4); package obj2(); package obj3(2); getchar(); return 0;}

Prog. Output: 42

D……………………………………………..#include<iostream> int main(){ char word[5]; cout<<”enter a word: “; cin.width(5); cin>>word; cout<<”you entered” << word << endl; getchar(); return 0;}Ans.Correct prog.

#include<iostream> using namespace std;int main(){ char word[5]; cout<<"enter a word: "; cin.width(5); cin>>word; cout<<"you entered" << word << endl; getchar(); return 0;}

Prog. Output:You enteredanyIt will check that length of string should be less than 5.

Page 16: Assignment Solved

Question 7: Users of the computer have profile which consists of Name, Password, and Access Rights. Access rights take on values X, R, W, and ALL. It is possible to have default values for Password and Access rights which are the first three letters of the Name and ALL respectively. Users can change their password and access rights.

Write a class User in C++ and create a user named Rajesh. Provide three ways in which Users can be created

by supplying Name only and using default values for the others by supplying Name and password and using default value for Access rights by supplying all information

Ans.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

class user

{

char name[20];

char passwd[10];

char access[3];

public:

user(char *n);

user(char *n,char *p,char *a="all")

{strcpy(name,n);

strcpy(passwd,p);

strcpy(access,a);

}

Page 17: Assignment Solved

};

user::user(char *n)

{int i;

strcpy(name,n);

for(i=0;i<3;i++)

passwd[i]=name[i];

strcpy(access,"all");

}

void main()

{

user u("himanshu"),u2("casin","rash");

}

Question 8: A Corporation has six divisions each responsible for sales to different geographic locations. Design a divSales class that keeps sales data for a division , with the following members:-

* An array with four elements for holding four quarters of sales figure for the division.

* A private static variables for holding the total corporate sales for all divisions for the entire year.

* A member function that takes four arguments, each assumed to be the sales for a quarter. The value of the arguments should be copied into the array that holds the sales data. The total of the four arguments should be added to the static variable that holds the total yearly corporate sales.

* A function that takes an arguments within the range of 0 to 3. the argument to be used as a subscript into the division quarterly sales array. The function should return the value of the array element with that subscript.

Write a program that creates an array of six divSales objects using pointers. The program should ask the user to enter the sales for four quarter for each division. After the data is entered, the program should

Page 18: Assignment Solved

display a table showing the division sales for each quarter. The program should then display the total corporate sales for the year. Provide the Input validation in your code: only accept positive values for quarterly sales figures.

Ans.

#include<iostream.h>

#include<conio.h>

static int total;

struct div

{

int sales[4],did;

struct div *next;

};

class divsales

{

private:

struct div *s,*t,*l;

public:

divsales(){ s=NULL;};

void dsales(int a,int b, int c,int d,int e);

void show();

};

void divsales::show()

{

clrscr();

struct div *p=s;

Page 19: Assignment Solved

while (p!=NULL)

{

cout<<"division : "<<p->did<<endl;

cout<<"first quarter sales : "<<p->sales[0]<<endl;

cout<<"second quarter sales : "<<p->sales[1]<<endl;

cout<<"third quarter sales : "<<p->sales[2]<<endl;

cout<<"fourth quarter : "<<p->sales[3]<<endl;

cout<<"\n";

p=p->next;

}

cout<<"total yearly sales is "<<total;

getch();

}

void divsales::dsales(int a,int b,int c,int d,int e)

{

t = new div;

t->did = e;

t->sales[0]=a;

t->sales[1]=b;

t->sales[2]=c;

t->sales[3]=d;

t->next=NULL;

if(s==NULL)

Page 20: Assignment Solved

s=t;

else

{

l=s;

while(l->next!=NULL)

l=l->next;

l->next=t;

}

total=total+a+b+c+d;

}

int main()

{

divsales d1;

int i,a[6],b[6],c[6],d[6];

clrscr();

for(i=0;i<6;i++)

{

cout<<"enter value for first quarter of division"<<i+1<<endl;

cin>>a[i];

cout<<"enter value for second quarter of division"<<i+1<<endl;

cin>>b[i];

cout<<"enter value for third quarter of division"<<i+1<<endl;

cin>>c[i];

Page 21: Assignment Solved

cout<<"enter value for fourth quarter of division"<<i+1<<endl;

cin>>d[i];

d1.dsales(a[i],b[i],c[i],d[i],i+1);

}

d1.show();

getch();

return 0;

}

Question 9: Implement the design pattern shown in diagram. Class SchoolMember is parent class. SchoolTeacher and SchoolStudent are child classes. Following are the relationship between these classes.

Ans.

#include<iostream.h>

#include<conio.h>

class schoolmember

{

protected:

char mname[20];

public:

schoolmember(){}

void tell();

};

Page 22: Assignment Solved

void schoolmember :: tell()

{

cout<<"\nthe member name is"<<mname;

}

class schoolteacher : public schoolmember

{

private:

char tname[20];

int tage;

public:

void tell();

void record_of_increment();

};

void schoolteacher:: record_of_increment()

{

int n,i,sal;

cout<<"enter the years employee worked in organisation"<<endl;

cin>>n;

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

{

sal=sal+ 0.1 * sal;

}

Page 23: Assignment Solved

cout<<"the current salary is"<<sal;

};

void schoolteacher:: tell()

{

cout<<"\nthe teacher age is :"<<tage;

cout<<"\nthe teacher name is: "<<tname;

}

class schoolstudent : public schoolmember

{

private:

char sname[20];

int sage,phy_marks;

public:

void tell();

void record_of_pcm_avg();

};

void schoolstudent:: record_of_pcm_avg()

{

int p,c,m,avg;

cout<<"enter physics marks"<<endl;

cin>>p;

cout<<"enter physics marks"<<endl;

cin>>c;

Page 24: Assignment Solved

cout<<"enter physics marks"<<endl;

cin>>m;

avg=(p+c+m)/300;

cout<<"pcm average is "<<avg;

}

void schoolstudent:: tell()

{

cout<<"\nthe student age is :"<<sage;

cout<<"\nthe student name is: "<<sname;

cout<<"\nstudent's physics marks are :"<<phy_marks;

}

class schoolstaff : public schoolmember

{

private:

char stname[20];

int stage;

public:

void tell();

void record_of_increment();

};

void schoolstaff:: record_of_increment()

{

Page 25: Assignment Solved

int n,i,sal;

cout<<"enter the years employee worked in organisation"<<endl;

cin>>n;

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

{

sal=sal+ 0.1 * sal;

}

cout<<"the current salary is"<<sal;

};

void schoolstaff:: tell()

{

cout<<"\nthe staff age is :"<<stage;

cout<<"\nthe staff name is: "<<stname;

}

int main()

{

schoolmember m;

clrscr();

getch();

return 0;

}

Page 26: Assignment Solved

Following is the functionality of function defined in classes:

Tell() //display values of member variables e.g. Tell() function in SchoolMember will display values of Name and age

Record_of_increment() //ask from user when an employee joined the organization. Then, it calculate number of years, he worked with organization. For each year it will increment salary by 10% and then display the current salary. Record_of_PCM_avg() //ask from student the physics,chemistry and mathematic marks and diplay it’s average

Ques 2. Double ended queue is a linear list in which additions and deletions may be made at either end. Implement class Dequeue as a publicly derived class of Queue created earlier. The Dequeue class should make use of overriding to redefine add and delete elements from either end of the Dequeue and also to return element from either end.

Question 10:.Design a software for a manufacturer that produces ‘planes’ and ‘ships’. Your S/w has to keep a record of Price, Id, Date of Manufacture for each of the products manufactured. Apart from this each plane also has a record of its ‘wingSpan’ , ‘weight’ And for ships its records the ‘volume’ and ‘length’ Write a program to record the data for this software using the concept of inheritance.

Now suppose this company came up with a hybrid of plane and ship. Your task is now to add another class ‘hybrid’ which will have properties of planes and ships.

Ans.

SchoolMember

-Name: String

-Age:IntegerTell()

SchoolTeacher

-Name: String

-Age:Integer

-Salary:Integer=8000Tell()

Record_of_increment()

SchoolStudent

-Name: String

-Age:Integer

-phy_Marks:Integer=0

-che_marks:Integer=0

-math_marks:Integer=0

Tell()

Record_of_PCM_avg()

SchoolStaff

-Name: String

-Age:Integer

-Salary:Integer=0Tell()

Record_of_increments()

*

1

1

**

1

Page 27: Assignment Solved

#include<iostream.h>

#include<conio.h>

class product

{

protected:

int id,price;

public:

};

class ships : public product

{

private:

int vol,len;

public:

void get_ships_data(int a,int b, int c, int d);

void sshow();

};

void ships :: sshow()

{

cout<<"ship id is : "<<id<<endl;

cout<<"ship price is : "<<price<<endl;

cout<<"ship volume are : "<<vol<<endl;

Page 28: Assignment Solved

cout<<"ship length is : "<<len<<endl;

}

void ships :: get_ships_data(int a,int b,int c,int d)

{

id =a;

price=b;

vol=c;

len=d;

}

class planes : public product

{

private:

int wing,weight;

public:

void get_planes_data(int a,int b,int c,int d);

void pshow();

};

void planes :: pshow()

{

cout<<"plane id is : "<<id<<endl;

cout<<"plane price is : "<<price<<endl;

cout<<"plane wings are : "<<wing<<endl;

cout<<"plane weight is : "<<weight<<endl;

}

Page 29: Assignment Solved

void planes :: get_planes_data(int a,int b,int c,int d)

{

id =a;

price=b;

wing=c;

weight=d;

}

class hybrid : public planes,public ships

{

};

int main()

{

int i,id,p,v,l,w,we;

planes p1[4];

ships s1[4];

clrscr();

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

{

cout<<"enter id"<<endl;

cin>>id;

cout<<"enter price"<<endl;

cin>>p;

cout<<"enter volume"<<endl;

cin>>v;

cout<<"enter length"<<endl;

Page 30: Assignment Solved

cin>>l;

s1[i].get_ships_data(id,p,v,l);

}

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

{

cout<<"enter id"<<endl;

cin>>id;

cout<<"enter price"<<endl;

cin>>p;

cout<<"enter wing"<<endl;

cin>>w;

cout<<"enter weight"<<endl;

cin>>we;

p1[i].get_planes_data(id,p,w,we);

}

p1[0].pshow();

cout<<"\n\n";

p1[1].pshow();

cout<<"\n\n";

s1[0].sshow();

cout<<"\n\n";

s1[1].sshow();

getch();

return 0;

}

Question 11: Identify the attributes and member functions for the following hierarchy.

Page 31: Assignment Solved

SOLUTION:

Community Member

Employee Student Alumnus

Faculty Staff

Administrator Teacher

Administrator Teacher

Page 32: Assignment Solved

Question 12. Define a class 'rational' with two private members Integers and Floats and a private functions 'display' that will display the numbers and getNumber() .There is another class arithmetic having two private functions 'add(rational A,rational B)' and 'substract(rational A,rational B)'. Your task is is to get two numbers from the user and perform add and substarct operation. Use of constructor in not allowed.(Hint: Read the above example)

Ans.

#include<iostream>

using namespace std;

class rational

{

int integers;

float floats;

void getnum(int x,float y)

{

integers=x;

floats=y;

}

void display()

{

cout<<"Integer part = "<<integers<<endl<<"float part = "<<floats<<endl;

}

public:

rational()

{}

int get_int()

{

Page 33: Assignment Solved

return integers;

}

float get_float()

{

return floats;

}

void call_getnum(int c,float v)

{

getnum(c,v);

}

void call_display()

{

display();

}

};

class arithmatic

{rational c;

int x1,x2,x3;

float y1,y2,y3;

public:

rational add(rational a,rational b){

x1=a.get_int();

x2=b.get_int();

x3=x2+x1;

y1=a.get_float();

y2=b.get_float();

y3=y2+y1;

Page 34: Assignment Solved

c.call_getnum(x3,y3);

return c;

}

rational sub(rational a,rational b){

x1=a.get_int();

x2=b.get_int();

x3=x2-x1;

y1=a.get_float();

y2=b.get_float();

y3=y2-y1;

c.call_getnum(x3,y3);

return c;

}

};

int main()

{

rational a,b,c,d;

a.call_getnum(2,6.8);

b.call_getnum(32,62.38);

arithmatic ar;

cout<<"1st rational number"<<endl;

a.call_display();

cout<<"2st rational number"<<endl;

b.call_display();

c=ar.add(a,b);

Page 35: Assignment Solved

d=ar.sub(b,a);

cout<<"addition result"<<endl;

c.call_display();

cout<<"sub result(b-a)"<<endl;

d.call_display();

d=ar.sub(a,b);

cout<<"sub result(a-b)"<<endl;

d.call_display();

getchar();

}

Question 13. There are two kinds of devices, input and output. The former keeps all data contained between blanks in an array. The latter outputs all data contained between blanks on. Each piece of data is on a separate line. A device is an integrated input-output device. It is possible to lock this device so that it is unavailable for use. Unlocking can be done only when the correct password is supplied. Draw an class structure and write a program to implement the foregoing.

Question 14. There is a class Item that records the title and price and a class Sales that record the sales made of the item in last three months. Design and implement the inheritance hierarchy so as to model (a) Hardware items and (b) Software items. Each of these can have their own additional features to differentiate. Demonstrate the usage and order of Constructors invocation in the program.

Ans.

#include<iostream>

#include<string.h>

using namespace std;

Page 36: Assignment Solved

class item

{

private :

char title[20];

int price;

public:

item()

{

price=0;

}

item(char* s1,int p1)

{

strcpy(title,s1);

price = p1;

}

void display1()

{

cout<<"Title:"<<title<<" price:"<<price<<endl;

}

};

class sales: public item

{

private:

int sale;

public:

Page 37: Assignment Solved

sales(int s,char* s1,int p):item(s1,p)

{

sale=s;

}

void display2()

{

cout<<"sales :"<<sale<<endl;

}

};

class software: public sales

{

private:

int bits;

float version;

public:

software(int b,float v,int s,char* s1,int p):sales(s,s1,p)

{

bits=b;

version=v;

}

void display3()

{

cout<<"Bits :"<<bits<<" version :"<<version<<endl;

}

};

class hardware: public sales

Page 38: Assignment Solved

{

private:

int processor;

public:

hardware(int p1,int s,char* s1,int p):sales(s,s1,p)

{

processor=p1;

}

void display3()

{

cout<<"Processor :"<<processor<<endl;

}

};

int main()

{

hardware h1(3,45,"himanshu_pad",10);

h1.display1();

h1.display2();

h1.display3();

getchar();

return 0;

}

Question 15: Find the output of the following :-

Page 39: Assignment Solved

(a). #include<iostream.h> using namespace std; class space{ int x,y,z; public: void getdata(int a,int b,int c); void display(void); void operator-(); }; void space :: getdata(int a,int b,int c) { x=a; y=b; z=c; } void space :: display(void) { cout<< x<<” “; cout<< y<<” “; cout<< z<<”\n “; } void space :: operator-() { x=-x; y=-y; z=-z; } int main() { space s; s.getdata(10,-20,30); cout<< “s :”; s.display(); -s; cout<<” s :”; s.display(); return 0;

}

Ans. S: 10 -20 30 S: -10 20 -30 Operator – overloaded just multiply the number by -1.

b)#include<iostream>

Page 40: Assignment Solved

using namespace std;class complex{ float x,y; public: complex(){ } complex(float real, float img) { x=real; y=img; } complex operator+(complx); void display(void);};

complex complex :: operator+(complx c){ complx t; t.x= x+c.x; t.y= y + c.y; return(t);}

void complex :: display(void){ cout << x << “+j” <<y <<”\n”;}

int main(){ complex c1,c2,c3; c1= complex(2.5,3.5); c2 = complex(1.6,2.7); c3 = c1+c2; cout<< “c1 = “;c1.display(); cout<< “c2 = “;c2.display(); cout<< “c3 = “;c3.display(); return 0;

}Ans. Correct program:

#include<iostream>using namespace std;

Page 41: Assignment Solved

class complex{ float x,y; public: complex(){ } complex(float real, float img) { x=real; y=img; } void display(void); complex operator + (complex); };

complex complex :: operator+(complex c){ complex t; t.x= x+c.x; t.y= y + c.y; return(t);}

void complex :: display(void){ cout << x << "+j" <<y <<"\n"; getchar();}

int main(){ complex c1,c2,c3; c1= complex(2.5,3.5); c2 = complex(1.6,2.7); c3 = c1+c2; cout<< "c1 = ";c1.display(); cout<< "c2 = ";c2.display(); cout<< "c3 = ";c3.display(); return 0; getchar();

}

Page 42: Assignment Solved

OUTPUT: c1=2.5 +j3.5 C2=1.6 +j2.7 C3 =4.1 +j6.2 Operator + is used to add two complex type object that return complex type object

Find the error in the following code:-

(a).class point{private: int x1 ,y1;public: point(int x, int y) { x1=x; y1=y; } void operator+(const &point right) { x1 +=right.x1; y1 +=right.y1; }};

Ans.Correct program#include<iostream>

using namespace std;

class point{private: int x1 ,y1;public: point(int x, int y) { x1=x; y1=y; } void operator + (const point & right) { x1 +=right.x1; y1 +=right.y1;

Page 43: Assignment Solved

} void display() { cout<< x1<<" "<<y1<<endl; getchar(); } };

int main (){ point p1(3,5); point p2(4,9); p2+p1; p1.display(); p2.display(); return 0;} b)class box{ private: float wi, le, he; public: box(float w,l,h) { wi=w; le=l; he=h; } void operator++() { ++wi; ++le; } void operator++() { wi++; le++; }};

Ans. Correct program

#include<iostream>

using namespace std;

class box

Page 44: Assignment Solved

{ private: float wi, le, he; public: box(float w,float l,float h) { wi=w; le=l; he=h; } void operator++() { ++wi; ++le; } void operator++(int x) { wi++; le++; } void display() { cout<<wi<<" "<<le<<" "<<he<<endl; getchar(); } };

int main(){ box b1(10,20,30); b1.display(); b1++; b1.display(); ++b1; b1.display(); return 0;}

(c).class yard { private: float le; public: yard(float l) { le=l; } void operator float()

Page 45: Assignment Solved

{ return le; }};

Ans. Void operator float float func_name () {return le;}

Function of class can not have name operator.