0% found this document useful (0 votes)
40 views

Visibility Modes in Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Visibility Modes in Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Visibility

Modes in
Inheritance
Content
s
Which are What is
What is
different visibility of
protected
visibility class
mode?
modes? members?

What is effect
of visibility Assignment /
modes in Q&A
Exercise
Inheritance
with
Learn
Visibility
Modes

private public protected


mode mode mode

The private data members are The public data members The protected data
not accessible outside the are accessible outside the members are accessible by
class. They are accessible by class also. They are member functions within
member functions of its own accessible by member its class and within a class
class only. In this way data functions and objects of its which is immediately
hiding is implemented own class. derived from it.
Visibility
Modes
class A class can use all three visibility modes for
base declaring
{ its members
private: Members are visible to member functions within its class
………
………
protected: Members are visible to member functions of its own class
………. and of its derived class
……….
public: Members are visible to all functions & objects of class
………
………
};
Visibility
Modes
class B class D : public B
{ { ‘a’ is private
private: private: member of class
int a ; int total; ‘B’ , hence not
protected public: accessible in
: void put_total (void) class ‘D’
float b; {
public: total = a + b; // Not allowed
void getdata(int x, float y) ‘b’ is protected
total = b + 100 ;
{ member of class
cout << total ;
} a = x; b = y; ‘B’ , hence
}
}; accessible in its
};
derived class ‘D’
C++ program to implement Multilevel
Inheritance

Problem Definition: Class: Student


Data: Roll_no, Name
Write a C++ program to implement

the multilevel inheritance as shown


Class: Test
in fig. Accept Roll_no, Name and Data: marks1, marks2

marks of student and display total


Class: Result
marks using protected mode.
Data: Total
C++ program to implement Multilevel
Inheritance void put_student(void)
#include <iostream.h> {
//Base Class cout<<“\nRoll No.: ”<<Roll_no;
class Student cout<<“\nName: “ << Name;
{ //Data member }
int Roll_no; }; //End of base class
char Name[15];
public: //Intermediate Base class Protected data
//member functions class Test : public Student , accessible in
void get_student(void) { protected: derived class
{ //Data members
cout<<“Enter Roll int marks1, marks2;
No.:”; cin>>Roll_no; public:
cout<<“Enter Name:”; //member functions
gets(name); void get_marks(void);
} }; //End of intermediate base class
C++ program to implement Multilevel
Inheritance
//Derived class
//member function to accept marks class Result: public Test
void Test :: get_marks(); { private:
{ //Data members
cout<<“Enter Marks of two subjects:”; int total;
cin>>marks1>>marks2; public:
} //member functions
void put_total(void)
{
total = marks1 + marks2;
marks1 & marks2 cout<<“Total marks=
are protected
“<<total;
members , hence
accessible in }
derived class }; //End of Derived class
C++ program to implement Multilevel
Inheritance
Main Program Output
int main()
{
Result R ; // Object of Derived class Enter Data of student:
cout << “Enter Data of Enter Roll No.: 75
R.get_student(); //Base class function Call
student:\n”; Enter Name: Sanjay Patil
R.get_marks(); //Intermediate base class function Call Enter Marks of two subjects: 22 20

cout << “Data of Data of student:


student:\n”;
R.put_student() //Base class function Call Roll No.: 75
; R.put_total(); //Derived class function Call Name: Sanjay Patil
return 0; Total Marks = 42
}
Question- Answer
Session
1. How may visibility modes are in C++ ?
2. Identify the visibility mode with the help of following description:
a) The class members are accessible within and outside the class also.
b) The class members are accessible within the class only, they are not

accessible outside the class.

c) The class members are accessible within the class and its immediate
derived class
3. Which mode supports the Data hiding property of OOP?
4. Which mode is useful to inherit the data members of base class into derived
class?
Assignment /
Exercise
Class: Student
Problem Definition: Data: Roll_no, Name
Write a C++ program to implement

the multilevel inheritance as shown in Class: Marks


Data: sub1, sub2,sub3,
fig. Accept roll no., name, marks of 5 sub4, sub5

subjects and display roll no., name,


Class: Result
total marks & percentage ofone Data: total, percent
display_total()
student. Display_precent()

You might also like