Sponsor Area
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.
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.
Write the definition of a function Reverse(int Arr[], int N) in C++, which should reverse the entire content of the array Arr having N elements, without using any other array.
Example: if the array Arr contains
13 | 10 | 15 | 20 | 5 |
Then the array should become
5 | 20 | 15 | 10 | 13 |
NOTE:
void printarray(int arr[], int count)
{
for(int i = 0; i < count; ++i)
cout<<arr[i]<<' ';
cout<<'\n';
}
void reverse(int arr[], int count)
{
int temp;
for (int i = 0; i < count/2; ++i)
{
temp = arr[i];
arr[i] = arr[count-i-1];
arr[count-i-1] = temp;
}
}
int main ()
{
clrscr();
const int SIZE = 10;
int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout<<'Input Array\n';
printarray(arr, SIZE);
reverse(arr, SIZE);
cout<<'Reverse Array\n';
printarray(arr, SIZE);
getch();
}
Write a definition for a function ADDMIDROW(int MAT[][10], int R, int C) in C++, which finds sum of the middle row elements of the matrix MAT (Assuming C represents the number of Columns and R represents the number of rows, which is an odd integer).
For example, if the content of array MAT having R as 3 and C as 5 is as follows:
1 | 2 | 3 | 4 | 5 |
2 | 1 | 3 | 4 | 5 |
3 | 4 | 1 | 2 | 5 |
The function should calculate the sum and display the following:
Sum of Middle Row: 15
void ADDMIDROW(int MAT[][10],int R,int C)
{
int MIDR=0;
for (int J=0;J<C;J++)
{
MIDR+=MAT[R/2][J];
cout<<'Sum of Middle Row:'<<MIDR<<endl;
}
}
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.
classMyClass
{
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 TEST
{
long TCode;
char TTitle[20];
float Score;
public:
TEST() //Member Function 1
{
TCode=100;strcpy(TTitle,”FIRST Test”);Score=0;
}
TEST(TEST &T) //Member Function 2
{
TCode=E.TCode+1;
strcpy(TTitle,T.TTitle);
Score=T.Score;
}
};
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 TEST?
(ii) Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively.
(i) Polymorphism OR Constructor overloading is illustrated by the Member Function 1 and Member Function 2 together in the class TEST.
(ii) TEST T1; //Statement 1
TEST T2(T1); //Statement 2
Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included:
class FICTION
{
long FCode;
char FTitle[20];
float FPrice;
public:
FICTION() //Member Function 1
{
cout<<'Bought'<<endl;
FCode=100;strcpy(FTitle,'Noname');FPrice=50;
}
FICTION(int C,char T[],float P) //Member Function 2
{
FCode=C;
strcpy(FTitle,T);
FPrice=P;
}
void Increase(float P) //Member Function 3
{
FPrice+=P;
}
void Show() //Member Function 4
{
cout<<FCode<<':'<<FTitle<<':'<<FPrice<<endl;
}
~FICTION() //Member Function 5
{
cout<<'Fiction removed!'<<end1;
}
};
void main() //Line 1
{ //Line 2
FICTION F1,F2(101,'Dare',75); //Line 3
for (int I=0;I<4;I++) //Line 4
{ //Line 5
F1.Increase(20);F2.Increase(15); //Line 6
F1.Show();F2.Show(); //Line 7
} //Line 8
} //Line 9
How many times the message 'Fiction removed!' will be displayed after executing the above C++ code? Out of Line 1 to Line 9, which line is responsible to display the message 'Fiction removed!'?
Two times the message 'Fiction removed!' will be displayed after execution.
Line 9 is responsible to display the message 'Fiction removed!'.
Write the definition of a class METROPOLIS in C++ with the following description:
Private Members
-Mcode //Data member for Code (an integer)
-MName //Data member for Name (a string)
-MPop //Data member for Population (a long int)
-Area //Data member for Area Coverage (a float)
-PopDens //Data member for Population Density (a float)
CalDen()//A member function to calculate
//Density as PopDens/Area
Public Members
-Enter() //A function to allow user to enter values of
//Mcode,MName,MPop,Area and call CalDen()
//function
-ViewALL()//A function to display all the data members
//also display a message ”Highly Populated Area”
//if the Density is more than 12000
class METROPOLIS
{
int Mcode;
char MName[20];
long int MPop;
float Area;
float PopDens;
void CalDen();
public:
void Enter();
void ViewALL();
};
void METROPOLIS::Enter()
{
cin>>Mcode;
gets(MName); // OR cin>>MName;
cin>>MPop;
cin>>Area;
CalDen();
}
void METROPOLIS::ViewALL()
{
cout<<Mcode<<MName<<MPop<<Area<<PopDens;
if(PopDens>12000)
cout<<”Highly Populated Area”;
}
void METROPOLIS::CalDen()
{
PopDens= PopDens/Area;
}
Sponsor Area
Sponsor Area