Computer Science With C++ Chapter 5 Inheritance (Extending Classes)
  • Sponsor Area

    NCERT Solution For Class 12 Computer And Communication Technology Computer Science With C++

    Inheritance (Extending Classes) Here is the CBSE Computer And Communication Technology Chapter 5 for Class 12 students. Summary and detailed explanation of the lesson, including the definitions of difficult words. All of the exercises and questions and answers from the lesson's back end have been completed. NCERT Solutions for Class 12 Computer And Communication Technology Inheritance (Extending Classes) Chapter 5 NCERT Solutions for Class 12 Computer And Communication Technology Inheritance (Extending Classes) Chapter 5 The following is a summary in Hindi and English for the academic year 2021-2022. You can save these solutions to your computer or use the Class 12 Computer And Communication Technology.

    Question 3
    CBSEENCO12011716

    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.

    Solution

     

    Private Public
    Private members of a class are accessible only from within other members of the same class or from their friends. Protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
    #include <iostream> 
    class Example
    {
    public:
    	int a;
    	int add(); private:
    	int b;
    };
    	int Example::add()
    {
    	return a+b ;
    }
    void main( )
    {
    	Example ex;
    	ex.a = 10; // OK: because a is public
    	ex.b = 20; // Error: because b is private
    	int sum=ex.add(); // local variable
    	cout << 'Sum of a + b : ' <<
    }
    Output: Error due to access of
    private member
    
    #include <iostream.h> 
    class ExBase
    {
    	protected: int i, j;
    };
    
    class ExDerived : public ExBase { public:
    	void show()
    	{
    		i=35; j=45;
    		//both i & j are accessible here cout<<'Value of i '<<i; cout<<'Value of j '<<j;
    	}
    };
    void main()
    {
    	ExDerived exd; exd.show();//both I and j are not accessible exd.i=50;
    	exd.j=60;
    }
    Question 5
    CBSEENCO12011718

    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.

    Solution
    class RESTRA
    {
    	int FoodCode;
    	char Food[20];
    	char FType[20];
    	char Sticker[20];
    	void GetSticker();
    public:
    	void GetFood();
    	void ShowFood();
    };
    void RESTRA::GetFood()
    {
    	cin>>FoodCode;
    	cin>>Food;
    	cin>>FType;
    	GetSticker ();
    }
    void RESTRA:: GetSticker ()
    {
    	if (strcmp(FType,'Vegetarian'))
    		strcpy(Sticker,'GREEN');
    	else if (strcmp(FType,'Contains Egg'))
    		strcpy(Sticker,'YELLOW');
    	else if(strcmp(FType,'Non-Vegetarian'))
    		strcpy(Sticker,'RED');
    }
    void RESTRA:: ShowFood ()
    {
    	cout<< FoodCode <<'\t'<<Food<<'\t'<<FType<<'\t'<<Sticker<<endl;
    }

    Mock Test Series

    Sponsor Area

    Sponsor Area

    NCERT Book Store

    NCERT Sample Papers

    Entrance Exams Preparation

    26