0% found this document useful (0 votes)
6 views

2014 - CE104 OOPC++ 2nd - Internal Solution

Uploaded by

AneriMehta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

2014 - CE104 OOPC++ 2nd - Internal Solution

Uploaded by

AneriMehta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Candidate seat No: _______________

Charotar University of Science and Technology [CHARUSAT]


Faculty of Technology and Engineering
Subject: CE104 Object Oriented Programming with C++
Second Internal Exam

Semester: 2nd Sem B. Tech. (CE/IT/EC) Maximum Marks: 30


Date: 11/04/2014 (Friday) Time: 10:00 to 11:00 a.m.
Instructions:
(i) Attempt all the questions.
(ii) Figures to the right indicate full marks.
(iii) Make suitable assumptions and draw neat figures wherever if required.

Q- Answer the following questions. [06


1 1. Explain abstract base class and pure virtual function. ]
(a)
2. Differentiate: early binding vs. late binding.
3. Explain the two ways to create a file in C++ with example.
Create a class Money having integer rupee, paisa as data. Define appropriate functions [05
(b to enter and display data. Also define function that supports the statement: obj1=115. ]
) Here obj1 is object of class and 115 is integer that represents paisa. After execution of
the statement obj1 will be 1 rupee 15 paisa.

#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.

Q- Answer the following questions (ANY THREE). [09


2 1. Explain this pointer in detail. ]
(a) 2. Draw the hierarchy of iostream and fstream classes.
3. Show how to use base class parameterized constructor into derived class.
4. In which situation virtual base class is mandatory? Show with example.
5. Explain the basic ios format functions for console I/O operations.

(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 getdata()


{

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

This program can also be done with pure virtual function


In that case class vehicle will become pure virtual function.

#include<iostream>
using namespace std;
class vehicle
{
protected:
float avg;
public:

virtual void getdata()=0;


virtual void putdata()=0;
};
class car: public vehicle
{
char ftcar[10];
int nw_car;

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.

Here constructor has been defined in INT class.


#include<iostream>
using namespace std;
class FLT
{
float F;
public:
FLT(float f)
{
F=f;
}
float getvalue()
{
return F;
}
};
Page 7 of 10
class INT
{
int I, FR;
public:
INT()
{
I=0;
FR=0;
}
INT (FLT ff)
{
I=(int)ff.getvalue();
FR= (ff.getvalue() - I)*100;
}
void display()
{
cout<<"Integer Part = "<<I<<endl;
cout<<"Fraction Part = "<<FR<<endl;
}
};

int main()
{
INT iobj;
FLT fobj(3.78);
iobj=fobj;
iobj.display();
}

Same program can be done by defining Operator function in class FLT.

#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.

for example in the above example


iobj=fobj;

INT is the destination class and FLT is the source class.

then u have to define FLT class first.

Similarly , if u are writing a operator function in the source class then u have to define
destination class first.

for example in the above example


iobj=fobj;

INT is the destination class and FLT is the source class.

If u are writing a operator function in the source class FLT then u have to define INT class
first.

*********

Page 10 of 10

You might also like