Sponsor Area
Object Oriented Programming
Differentiate between private and public members of a class in the context of Object Oriented Programming. Also, give a suitable example illustrating accessibility/non-accessibility of each using a class and an object in C++.
A class can have multiple public, protected, or private access modifier.
A public class of member or function is accessible from anywhere outside the class but within the program. You can set and get the value of public variables without any member function as shown in the given example.
A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.By default, all the members of a class would be private, for example.
{
public:int a;
private:int b;
};
int main()
{
MyClass obj;
obj.a =10;//Allowed
obj.b=30;//Not Allowed, gives compiler error}
Observe the following C++ code and answer the questions (i) and (ii).
Note: Assume all necessary files are included.
class EXAM
{
long Code;
char EName[20];
float Marks;
public:
EXAM() //Member Function 1
{
Code=100;strcpy(EName,'Noname');Marks=0;
}
EXAM(EXAM &E) //Member Function 2
{
Code=E.Code+1;
strcpy(EName,E.EName);
Marks=E.Marks;
}
};
void main()
{
___________________ //Statement 1
___________________ //Statement 2
}
(i) Which Object-Oriented Programming feature is illustrated by the Member Function 1 and Member Function 2 together in the class EXAM?
(ii) Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively.
(i) 'Polymorphism or Constructor overloading or Function overloading' feature is illustrated by the member Function 1 and Member Function 2 together in the class Exam.
(ii) EXAM E1; //Statement 1
EXAM E2(E1); //Statement 2
Write the definition of a class RING in C++ with the following description:
Private Members
- RingNumber // data member of integer type
- Radius // data member of float type
- Area // data member of float type
- CalcArea() // Member function to calculate and assign
// Area as 3.14 * Radius*Radius
Public Members
- GetArea() // A function to allow user to enter values of
// RingNumber and Radius. Also, this
// function should call CalcArea() to calculate
// Area
- ShowArea() // A function to display RingNumber, Radius
// and Area
class RING
{
int RingNumber;
float Radius;
float Area;
void CalcArea(){Area=3.14*Radius*Radius;
}
public:
void GetArea();
void ShowArea();
};
void RING::GetArea()
{
cin>>RingNumber>>Radius;
CalcArea();
}
void RING::ShowArea()
{
cout<<RingNumber<<' '<<Radius<<' '<<Area<<endl;
}
Answer the questions (i) to (iv) based on the following:
class One
{
int A1;
protected:
float A2;
public:
One();
void Get1(); void Show1();
};
class Two :
private One
{
int B1;
protected:
float B2;
public:
Two();
void Get2();
void Show();
};
class Three : public Two
{
int C1;
public:
Three();
void Get3();
void Show();
};
void main()
{
Three T; //Statement 1
_______; //Statement 2
}
(i) Which type of Inheritance out of the following is illustrated in the above example?
-Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance.
(ii) Write the names of all the member functions, which are directly accessible by the
object T of class Three as declared in main() function.
(iii) Write Statement 2 to call function Show() of class Two from the object T of Class Three.
(iv) What will be the order of execution of the constructors, when the object T of
class Three is declared inside main()?
(i) Multilevel Inheritance is illustrated in the above example.
(ii) Get3(), Show() of class Three
Get2(), Show() of class Two
These are directly accessible by the object T of class Three as declared in main() function.
(iii) T.Two::Show()
(iv) when the object T of class Three is declared inside main() then the order of the construction is One, Two, Three.
Sponsor Area
Mock Test Series
Mock Test Series



