Object Oriented Programming
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
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++.
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.
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
Which specific concept of object-oriented programming out of the following is illustrated by Member Function 1 and Member Function 2 combined together?
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!'?
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
Sponsor Area
Sponsor Area