-->

Inheritance (Extending Classes)

Question
CBSEENCO12011670

Write the definition of a function Alter(int A[], int N) in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0. For example, if an array of 10 integers is as follows:

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
55 45 20 16 39 90 83 40 48 25

After executing the function, the array content should be changed as follows:

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
5 0 5 0 0 5 0 5 0 5

Solution
void Alter(int A[ ],int N)
{
	for (int i=0;i<N;i++){
		if(A[i]%5==0)
			A[i]=5;
		else
			A[i]=0;
	}
}

Some More Questions From Inheritance (Extending Classes) Chapter

Answer the questions (i) to (iv) based on the following:

class First
{
	int X1;
protected:
	float X2;
public:
	First();
	void Enter1(); void Display1();
};
class Second : private First
{
	int Y1;
protected:
	float Y2;
public:
	Second();
	void Enter2();
	void Display();
};
class Third : public Second
{
	int Z1;
public:
	Third();
	void Enter3();
	void Display();
};
void main()
{
Third 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 Third as declared in main() function.

(iii)Write Statement 2 to call function Display() of class Second from the object T of class Third.

(iv) What will be the order of execution of the constructors, when the object T of class Third is declared inside main()?



Write the definition of a function Alter(int A[], int N) in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0. For example, if an array of 10 integers is as follows:

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
55 45 20 16 39 90 83 40 48 25

After executing the function, the array content should be changed as follows:

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
5 0 5 0 0 5 0 5 0 5

What is the difference between the members in private visibility mode and the members in protected visibility mode inside a class? Also, give a suitable C++ code to illustrate both.

Answer the question (i) and (ii) after going through the following class :

class Travel
{
	int PlaceCode; char Place[20]; float Charges; public:
Travel( )	//Function 1
{
	PlaceCode=1;strcpy(Place,”DELHI”);Charges=1000;
}
void TravelPlan(float C )	// Function 2
{
	cout<<PlaceCode<<“:”<<Place<<”:”<<Charges<<endl;
}
~Travel( )	// Function 3
{
	cout<<”Travel Plan Cancelled”<<endl;
}
Travel(int PC,char p[],float C)	// Function 4
{
	PlaceCode=PC;strcpy(Place,P);Charges=c;
}

(i) In Object Oriented Programming, what are Function 1 and Function 4 combined together referred as?

(ii) In Object Oriented Programming, which concept is illustrated by Function 3? When is this function called/invoked?

Define a class RESTRA in C++ with following description: Private Members:

  • FoodCode of type int
  • Food of type string
  • FType of type string
  • Sticker of type string

A member function GetSticker() to assign the following values for Sticker as per the given Ftype:

FType

Sticker

Vegetarian

GREEN

Contains Egg

YELLOW

Non-Vegetarian

RED

Public Members :

  • A function GetFood() to allow user to enter values for FoodCode, Food,Ftype and call function GetSticker() to assign
  • A function ShowFood() to allow user to view the concept of all the data members.

Answer the questions (i) to (iv) based on the following:

class COMPANY
{
	char Location[20]; double Budget,Income;
protected:
	void Accounts( ); public:
	COMPANY();
	void Register( ); void Show( );
};
class FACTORY: public COMPANY
{
	char Location[20];
	int Workers;
protected:
	double Salary;
	void Computer();
public:
	FACTORY();
	void Enter( );
	void show( );
};
class SHOP: private COMPANY
{
	char Location[20];
	float Area;
	double Sale;
public:
	SHOP();
	void Input();
	void Output();
};

(i) Name the type of inheritance illustrated in the above C++ code.

(ii) Write the names of data members, which are accessible from member functions of class SHOP.

(iii) Write the names of all the member functions, which are accessible from objects belonging to class FACTORY.

(iv) Write the names of all the members, which are accessible from objects of class SHOP.