You are on page 1of 9

National Institute of Technology, Raipur

B.Tech. (4th Semester) MOI Remedial Exam April 2019


Subject: Object oriented concepts & programming using C++(CS20413)
Branch: Computer Science & Engineering Max. Marks: 15

Note: Attempt any three questions. All questions carry equal marks.

1. What is inheritance? How will you make private member of a class inheritable? Explain ambiguity resolution in
inheritance.[1+2+2]
Solu: The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object Oriented Programming.
The class that inherits properties from another class is called Sub class or Derived Class.
The class whose properties are inherited by sub class is called Base Class or Super class.

Implementing inheritance in C++: For creating a sub-class which is inherited from the base class we have
to follow the below syntax.
Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
 Protected Inheritance means that the derived class can access private and public members of the
base class protectedly.
 Protected members of the base class are protected in the derived class.
 Private members of the base class remain private
 These are visible only in the base.

Ambiguity in multiple inheritances

Consider an example of multiple inheritances in which there are two base classes having functions
with the same name and a class derived from both these base classes having no function with this
name.

#include<iostream.h>

#include<conio.h>

Class A

Public:

Void show ()

Cout<<”\n class A”;

}
};

Class B

Public:

Void show ()

Cout<<”\n class B”;

};

Class C: public A, public B

};

Void main (0

C obj;

Obj. Show ();

2. Define virtual member. How members functions can be access with the help of pointer. Explain virtual function
access with derived class.[1+2+2]
Solu: A virtual function a member function which is declared within a base class and is re-defined(Overriden) by a
derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a
virtual function for that object and execute the derived class’s version of the function.
 Virtual functions ensure that the correct function is called for an object, regardless of the type of reference
(or pointer) used for function call.
 They are mainly used to achieve Runtime polymorphism.
 Functions are declared with a virtual keyword in base class.
 The resolving of function call is done at Run-time.

To access a member function by pointer, we have to declare a pointer to the object and
initialize it.
Syntax:

//pointer to object declaration


class_name *pointe_name;
//memory initialization at runtime
pointer_name = new class_name;
//accessing member function by using arrow operator
pointer_name->member_function();

Example:

In the below example - there is a class named Number with private data member num and
public member functions inputNumber(), displayNumber().

In the example, we are creating simple object N and a pointer to the object ptrN and
accessing the member functions by using simple object N and the pointer to the
object ptrN.

#include <iostream>
using namespace std;

class Number
{
private:
int num;
public:
//constructor
Number(){ num=0; };

//member function to get input


void inputNumber (void)
{
cout<<"Enter an integer number: ";
cin>>num;
}
//member function to display number
void displayNumber()
{
cout<<"Num: "<<num<<endl;
}
};

//Main function
int main()
{
//declaring object to the class number
Number N;
//input and display number using norn object
N.inputNumber();
N.displayNumber();

//declaring pointer to the object


Number *ptrN;
ptrN = new Number; //creating & assigning memory

//printing default value


cout<<"Default value... "<<endl;
//calling member function with pointer
ptrN->displayNumber();

//input values and print


ptrN->inputNumber();
ptrN->displayNumber();

return 0;
}

Output

Enter an integer number: 10


Num: 10
Default value...
Num: 0
Enter an integer number: 20
Num: 20
#include<iostream>
using namespace std;

class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }

void show ()
{ cout<< "show base class" <<endl; }
};

class derived:public base


{
public:
void print ()
{ cout<< "print derived class" <<endl; }

void show ()
{ cout<< "show derived class" <<endl; }
};

int main()
{
base *bptr;
derived d;
bptr = &d;

//virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
}
Output:

print derived class


show base class

3. Explain multiple inheritance with constructor and destructor operation executed with example. [5]

Solu: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.
The constructors of inherited classes are called in the same order in which they are inherited. For example, in the
following program, B’s constructor is called before A’s constructor.
#include<iostream>
using namespace std;

class A
{
public:
A() { cout << "A's constructor called" << endl; }
};

class B
{
public:
B() { cout << "B's constructor called" << endl; }
};

class C: public B, public A // Note the order


{
public:
C() { cout << "C's constructor called" << endl; }
};

int main()
{
C c;
return 0;
}
Output:
B's constructor called
A's constructor called
C's constructor called

4. Write a C++ program to read and print employee information with department and loan information using
hierarchical inheritance.
Solu:
5. include <iostream>
6. #include <stdio.h>
7. using namespace std;
8.
9. //Base Class - basicInfo
10. class basicInfo
11. {
12. protected:
13. char name[30];
14. int empId;
15. char gender;
16. public:
17. void getBasicInfo(void)
18. {
19. cout << "Enter Name: ";
20. cin.ignore(1);
21. cin.getline(name,30);
22. cout << "Enter Emp. Id: ";
23. cin >> empId;
24. cout << "Enter Gender: ";
25. cin >> gender;
26. }
27. };
28.
29. //Base Class - deptInfo
30. class deptInfo: private basicInfo
31. {
32. protected:
33. char deptName[30];
34. char assignedWork[30];
35. int time2complete;
36. public:
37. void getDeptInfo(void)
38. {
39. getBasicInfo(); //to get basic info of an
employee
40. cout << "Enter Department Name: ";
41. cin.ignore(1);
42. cin.getline(deptName,30);
43. cout << "Enter assigned work: ";
44. fflush(stdin);
45. cin.getline(assignedWork,30);
46. cout << "Enter time in hours to complete work:
";
47. cin >> time2complete;
48. }
49. void printDeptInfo(void)
50. {
51. cout << "Employee's Information is: " <<
endl;
52. cout << "Basic Information...:" << endl;
53. cout << "Name: " << name << endl;
//accessing protected data
54. cout << "Employee ID: " << empId << endl;
//accessing protected data
55. cout << "Gender: " << gender << endl <<
endl;//accessing protected data
56.
57. cout << "Department Information...:" <<
endl;
58. cout << "Department Name: " <<
deptName << endl; //accessing protected data
59. cout << "Assigned Work: " <<
assignedWork << endl; //accessing protected data
60. cout << "Time to complete work: " <<
time2complete<< endl; //accessing protected data
61.
62. }
63. };
64.
65. //another Base Class : loadInfo
66. class loanInfo:private basicInfo
67. {
68. protected:
69. char loanDetails[30];
70. int loanAmount;
71. public:
72. void getLoanInfo(void)
73. {
74. getBasicInfo(); //to get basic info of an
employee
75. cout << "Enter Loan Details: ";
76. cin.ignore(1);
77. cin.getline(loanDetails,30);
78. cout << "Enter loan amount: ";
79. cin >> loanAmount;
80. }
81. void printLoanInfo(void)
82. {
83. cout << "Employee's Information is: " <<
endl;
84. cout << "Basic Information...:" << endl;
85. cout << "Name: " << name << endl;
//accessing protected data
86. cout << "Employee ID: " << empId << endl;
//accessing protected data
cout << "Gender: " << gender << endl << endl;//accessing
protected data
87.
cout << "Loan Information...:" << endl;
88. cout << "Loan Details: " <<
loanDetails << endl; //accessing protected data
89. cout << "Loan Amount : " << loanAmount
<< endl; //accessing protected data
90. }
91. };
92.
93. int main()
94. {
95. //read and print department information
96. deptInfo objD;
97.
98. objD.getDeptInfo();
99. objD.printDeptInfo();
100.
101. cout << endl << endl ;
102. //read and print loan information
103. loanInfo objL;
104.
105. objL.getLoanInfo();
106. objL.printLoanInfo();
107.
108.
109. return 0;
110. }

You might also like