0% found this document useful (0 votes)
31 views24 pages

Answer Bank For OOP CT2

Uploaded by

temphakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views24 pages

Answer Bank For OOP CT2

Uploaded by

temphakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Practice Questions

Course: Object Oriented Programming using C++ Class:CO3I A/B/C

BEFORE THIS DOCUMENT NOTE THAT U ARE USING THIS AT UR OWN RISK AND I
AM NOT RESPONISBLE IF U GET LESS MARKS IN OOP

Questions:

1. List different access specifiers used in Inheritance and Describe use


of private access specifier used in the class.
Ans. Different Access Specifiers are as follows:
a. Public
b. Private
c. Protected
The class members declared as private can be accessed only by the
member functions inside the class. They are not allowed to be accessed
directly by any object or function outside the class. Only the member
functions or the friend functions are allowed to access the private data
members of the class.

2. List different access specifiers used in Inheritance and Describe use of


protected access specifier used in the class.
Ans. Different Access Specifiers are as follows:
Public
Private
Protected
The protected access modifier is similar to the private access modifier in
the sense that it can’t be accessed outside of its class unless with the help
of a friend class. The difference is that the class members declared as
Protected can be accessed by any subclass (derived class) of that class as
well.
3. List different access specifiers used in Inheritance and Describe use
of public access specifier used in the class.
Ans: Different Access Specifiers are as follows:
Public
Private
Protected
The class members declared as private can be accessed only by the
member functions inside the class. They are not allowed to be accessed
directly by any object or function outside the class. Only the member
functions or the friend functions are allowed to access the private data
members of the class.

4. Define multilevel inheritance. Draw the diagram to show multilevel


inheritance. using classes with data member and member function.
Ans:

The mechanism of deriving a class from another derived class is known as


Multilevel Inheritance.
5. Define multiple inheritance. Draw the diagram to show multiple
inheritance. using classes with data member and member function.
Ans:

A derived class with several base classes is called as multiple inheritance.

6. Differentiate between Compile time polymorphism and Run time


polymorphism.
Ans:

7. Define polymorphism. List its types with example.


Ans:
Polymorphism is the ability of an object to take on many forms. Types of
Polymorphism are as follows:
Compile Time Polymorphism and Run Time Polymorphism.
Example of Compile Time are Function Overloading and Operator Overloading
Example Of Run Time are Virtual Functions
8. Write rules for operator overloading.
Ans:

9. Give syntax to define operator function.


Ans:
10. Explain Pointer arithmetic with example.
Ans:
11. Write the use of following

w.r.t file

ios : : trunc and ios : : app

open() and close()

ios : : in and ios : : out

seekg() and seekp()

Ans:

Open: Used To Open a

File

Close(): Used To Close a

file
12. Give syntax to define derived class constructor and describe the order of
execution of constructor for following Figure.

Ans:
Syntax:
class derived-class_name : visibiliy_mode base_class_name

In the following figure class C will be executed first then Class B and then Class A
13. Describe the use of Virtual Function.
Ans:

14. Describe the use of pure Virtual Function.


Ans:

15. Describe the use of abstract base class.


Ans:
An Abstract class is one that is not used to create object. An abstract class is
designed only to act as a base class which is used to be inherited by other
classes. An abstract class has atleast one pure virtual function.

16. Give meaning of following


statements: int
a[5]={1,2,3,4,5};
int * ptr;
ptr = &a;
cout
<< *ptr<<”\n” ;
cout << ptr++;
cout << * ptr<<”\n” ;
Ans:
First we declare an integer array of size 5 with 1,2,3,4,5 in it, then we declare
and pointer integer named *ptr, then we set the address of a as data of ptr,
then we print the number the pointer is pointing at, then we increment the
pointer by 1 that is 1, then we print the incremented pointer the pointer is
pointing at that is 2.
17. Give meaning of following
statements: int * ptr, a = 5;
ptr = & a ;
cout << *
ptr ;
cout << (* ptr) + 1;
Ans:
First we declare integer pointer and a=5, then we point the pointer to the
address of a, then we print that value that is 5, then we add 1 to the
value which is printed as 6.

18. Write a C++ program to implement following in heritance. Refer following


Figure

Ans:
#include<iostream.h>
#include<conio.h>
class college
{
protected:
int college_code;
public:
void get_code(int a)
{
college_code=a;
}
void put_code()
{
cout<<”College Code: “college_code;
}
};
class test : public college
{
protected:
int percentage;
public:
void get_perc(int b)
{
percentage=b;
}
void put_data()
{
cout<<”Percentage: “<<percentage;
}
};
class sports : public college
{
protected:
char grade;
public:
void get_grade(char c)
{
grade=c;
}
void put_grade()
{
cout<<”Grade: “<<grade;
}
};
class result: public test, public sports
{
public:
void display()
{
put_code();
put_data();
put_grade();
}
};
void main()
{
result r1;
r1.get_code(0568);
r1.get_perc(90);
r1.get_grade(A);
r1.display();
getch();
}

19. Write a C++ program to implement following in heritance. Refer following


Figure
Ans:
#include<iostrem.h>
#include<conio.h>
class car
{
protected:
char car_type;
public:
void get_type(char a)
{
car_type=a;
}
void put_type()
{
cout<<”\n Car Type: “<<car_type;
}
};
class brand: public car
{
protected:
char brand_name;
int speed;
public:
void get_data1(char x, int y)
{
brand_name=x;
speed=y;
}
void put_data1()
{
cout<<”Brand Name: “<<brand_name;
cout<<”Speed : “<<speed;
}
};
class model : public brand
{
protected:
char model_name;
int price;
public:
void get_data2(char p, int n)
{
model_name=p;
price=n;
}
void display()
{
put_type();
put_data1();
cout<<”Model Name: “<<model_name;
cout<<”Price: “<<price;
}
};

void main()
{
model m1;
m1.get_type(Electric);
m1.get_data1(Tesla,120);
m1.get_data2(Model Y,5000000);
m1.display();
getch();
}

20. Write a program in C++ to overload unary ‘--’ operator to decrease


value of data member of class.
Ans:
#include<iostream.h>
class a
{
int b;
public:
void getdata()
{
cout<<”Enter a number: “;
cin>>b;
}
void operator –()
{
b--;
}
void display()
{
cout<<”Number: “<<b;
}
};
void main()
{
a a1;
a1.getdata();
-a1;
a1.display();
}
21. Write a program in C++ to overload unary ‘++’ operator to increase
value of data member of class.
Ans:
#include<iostream.h>
class a
{
int b;
public:
void getdata()
{
cout<<”Enter a number: “;
cin>>b;
}
void operator +()
{
b++;
}
void display()
{
cout<<”Number: “<<b;
}
};
void main()
{
a a1;
a1.getdata();
+a1;
a1.display();
}
22. Write a program in C++ to overload unary ‘-’ operator to negate
values of data members of class.
Ans:

23. Write a program in C++ to overload unary ‘+’ operator to concatenate two
strings.
Ans:
24. Write a C++ program to declare a class product with members as
product_no and name. Accept and display data for one object of product.
Use pointer to object to call functions of class.
Ans:
#include<iostream.h>
#include<conio.h>
class product
{
private:
int product_no;
char name;
public:
void getdata()
{
cout<<”Enter Product No and Name: “;
cin>>product_no>>name;
}
};
void product :: show_data()
{
cout<<”Product No: “<<product_no;
cout<<”Name: “<<name;
}
int main()
{
product p, *ptr;
ptr=&p;
p->show_data();
return 0;
}
25. Write a C++ program to declare a class employee with members as
employee_no and name. Accept the information for 5 objects and
display it using pointer to array of objects.
Ans:
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_no;
char name;
public:
void setdata(int a,char b)
{
emp_no=a;
name=b;
}
};
void employee :: showdata()
{
cout>>”Employee NO: ”>>emp_no;
cout>>”Name: “>>name;
}
int main()
{
int p,i;
char q;
employee e[5],*ptr;
for(i=0;i<5;i++)
{
cout<<”Enter Emp Number and Name”<<i+1<<endl;
cin>>p>>q;
ptr->setdata(p,q);
ptr++;
}
for(i=0;i<5;i++)
{
cout<<”Employee Details: “<<i+1<<endl;
ptr->showdata();
ptr++;
}
return 0;
}

26. Write a C++ program to write ‘Welcome to First lecture’ in a file. Then
read the data from file and display it on screen.
Ans:

27. Write a C++ program to copy content of one file to another file.
Ans:
28. Write a C++ program to calculate number of words in the “abc.txt” file.
Ans:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("fname.txt"); //opening text file
int word=1; //will not count first word so initial value is 1
char ch;
fin.seekg(0,ios::beg); //bring position of file pointer to begining of file

while(fin)
{
fin.get(ch);
if(ch==' '||ch=='\n')
word++;
}

cout<<"\nWords="<<word<<"\n";
fin.close(); //closing file
return 0;
}

29. Write a program to define add() function to add two integers and to add
three integers.

Ans:
30. Write a program to overload ‘+‘ operator to add two time objects
having data members as hours and minutes. Display the result using
third object.
Ans:
#include<iostream.h>
#include<conio.h>
class time
{
int hours,mins;
int result;
public:
void getdata()
{
cout<<”Enter Hours And Mins: “;
cin>>hours>>mins;
}
void operator +()
{
hours++;
mins++;
}
void display()
{
result=hours;
cout<<” Hours: “<<hours;
result=mins;
cout<<”Mins :”<<mins;
}
};
void main()
{
time t;
t.getdata();
+t;
t.display();
}

You might also like