2014 - CE104 OOPC++ 2nd - Internal Solution
2014 - CE104 OOPC++ 2nd - Internal Solution
#include<iostream>
using namespace std;
class Money
{
int rup, paisa;
public:
Money()
{
}
Money(int p)
{
rup = p/100;
paisa = p%100;
}
void put_amt()
{
cout<<"Amount in Rupees is "<<rup<<endl;
cout<<"Amount in Paisa is "<<paisa<<endl;
}
};
int main()
{
Money obj1;
obj1=115;
obj1.put_amt();
}
OUTPUT:
Page 1 of 10
OR
Overload << and >> operators for the above mentioned class.
#include<iostream>
using namespace std;
class Money
{
int rup, paisa;
public:
friend void operator >> (istream & ,Money &);
friend void operator << (ostream & , Money & );
};
void operator >> (istream & din,Money &m)
{
cout<<"Enter Ruppees :" ;
din>>m.rup;
cout<<"Enter Paisa :" ;
din>>m.paisa;
}
void operator << (ostream & dout, Money &m)
{
dout<<"Ruppess: "<<m.rup<<endl;
dout<<"Paisa: "<<m.paisa<<endl;
}
int main()
{
Money obj1;
cin>>obj1;
cout<<obj1;
}
OUTPUT:
Page 2 of 10
(c) Explain the command line argument in detail. [04
OR ]
Show the types of the inheritance with the help of figures.
(b Create a class vehicle having average as data. Derive class car and truck having date [06
) members: fuel type (petrol, diesel, CNG) and no of wheels respectively. Write a main ( ) ]
that enters the data of two cars and a truck and display the details of them. The class
Vehicle must have virtual function to enter and display the data.
#include<iostream>
using namespace std;
class vehicle
{
protected:
float avg;
public:
}
virtual void putdata()
{
Page 3 of 10
}
};
class car: public vehicle
{
char ftcar[10];
int nw_car;
public:
void getdata()
{
cout<<"Enter Average of Car: ";
cin>>avg;
cout<<"Enter Fuel-Type for car: ";
cin>>ftcar;
cout<<"Enter No of wheels for car : ";
cin>>nw_car;
}
void putdata()
{
cout<<" Average of Car is: "<<avg<<endl;
cout<<"Fuel-Type for car is : "<<ftcar<<endl;
cout<<"No of wheels for car is : "<<nw_car<<endl;
}
};
class truck: public vehicle
{
char fttruck[10];
int nw_truck;
public:
void getdata()
{
cout<<"Enter Average of Truck: ";
cin>>avg;
cout<<"Enter Fuel-Type for Truck: ";
cin>>fttruck;
cout<<"Enter No of wheels for Truck : ";
cin>>nw_truck;
}
void putdata()
{
cout<<" Average of Truck is: "<<avg<<endl;
cout<<"Fuel-Type for Truck is : "<<fttruck<<endl;
cout<<"No of wheels for Truck is : "<<nw_truck<<endl;
}
};
int main()
{
vehicle *list[2];
car c1;
truck t1;
list[0]=&c1;
Page 4 of 10
list[1]=&t1;
cout<<"Enter Car Details: "<<endl;
list[0]->getdata();
cout<<"Enter Truck Details: "<<endl;
list[1]->getdata();
cout<<".........Car Details........."<<endl;
list[0]->putdata();
cout<<".........Truck Details........."<<endl;
list[1]->putdata();
}
OUTPUT:
#include<iostream>
using namespace std;
class vehicle
{
protected:
float avg;
public:
Page 5 of 10
public:
void getdata()
{
cout<<"Enter Average of Car: ";
cin>>avg;
cout<<"Enter Fuel-Type for car: ";
cin>>ftcar;
cout<<"Enter No of wheels for car : ";
cin>>nw_car;
}
void putdata()
{
cout<<" Average of Car is: "<<avg<<endl;
cout<<"Fuel-Type for car is : "<<ftcar<<endl;
cout<<"No of wheels for car is : "<<nw_car<<endl;
}
};
class truck: public vehicle
{
char fttruck[10];
int nw_truck;
public:
void getdata()
{
cout<<"Enter Average of Truck: ";
cin>>avg;
cout<<"Enter Fuel-Type for Truck: ";
cin>>fttruck;
cout<<"Enter No of wheels for Truck : ";
cin>>nw_truck;
}
void putdata()
{
cout<<" Average of Truck is: "<<avg<<endl;
cout<<"Fuel-Type for Truck is : "<<fttruck<<endl;
cout<<"No of wheels for Truck is : "<<nw_truck<<endl;
}
};
int main()
{
vehicle *list[2];
car c1;
truck t1;
list[0]=&c1;
list[1]=&t1;
cout<<"Enter Car Details: "<<endl;
list[0]->getdata();
cout<<"Enter Truck Details: "<<endl;
list[1]->getdata();
cout<<".........Car Details........."<<endl;
list[0]->putdata();
Page 6 of 10
cout<<".........Truck Details........."<<endl;
list[1]->putdata();
}
OUTPUT:
OR
Create a class INT having integer I and FR as data. Another class FLT having float F as
data. Both the classes have constructor and appropriate functions for input and display
data. Write a program such that it supports:
INT iobj; FLT fobj(3.78);
iobj=fobj;
This operation will assign 3 to I and fraction part 78 to FR. When iobj is displayed, it
should print Integer part=3, Fraction part=78.
int main()
{
INT iobj;
FLT fobj(3.78);
iobj=fobj;
iobj.display();
}
#include<iostream>
using namespace std;
Page 8 of 10
class INT
{
public:
int I, FR;
INT()
{
I=0;
FR=0;
}
void display()
{
cout<<"Integer Part = "<<I<<endl;
cout<<"Fraction Part = "<<FR<<endl;
}
};
class FLT
{
float F;
public:
FLT(float f)
{
F=f;
}
operator INT ()
{
INT iobject;
iobject.I = (int)F ;
iobject.FR = (F-iobject.I)*100;
return iobject;
};
int main()
{
INT iobj;
FLT fobj(3.78);
iobj=fobj;
iobj.display();
}
OUTPUT:
Page 9 of 10
Note : In data conversion , if u are writing a constructor in the destination class then u
have to define source class first.
Similarly , if u are writing a operator function in the source class then u have to define
destination class first.
If u are writing a operator function in the source class FLT then u have to define INT class
first.
*********
Page 10 of 10