+ All Categories
Home > Documents > encapsulation and information hiding

encapsulation and information hiding

Date post: 09-Apr-2018
Category:
Upload: mathurmudit1003
View: 225 times
Download: 0 times
Share this document with a friend
72
Class and objects
Transcript

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 1/72

Class and objects

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 2/72

Topics for CAT II

Encapsulation and information hiding

Classes and subclasses

Separation of behavior and implementation

Modules in programming language

Inheritance

QUIZ II

Polymorphism

Collection class and protocol

Garbage collection Activation records

Object oriented design

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 3/72

Encapsulation and Information hiding

Encapsulation include behavior in the same container as data

encapsulation is the inclusion of one thing within anotherthing

the methods and the data

In Object Oriented Programming, encapsulation is anattribute of object design. It means that all of the object'sdata is contained and hidden in the object and access to itrestricted to members of that class.

 ± Public : All Objects can access it.

 ± Protected : Access is limited to members of the same class or descendants.

 ± Private : Access is limited to members of the same class.

 ± Internal : Access is limited to the current class

 ± Protected Internal : Access is limited to the current class or types derived from the containingclass.

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 4/72

Encapsulation refers to the binding of data

with the methods that operate on that data.

you can have encapsulated data that is not

hidden at all.

Hiding information in that manner isolates

uses from requiring knowledge of the design

to use a module and changing those decisions.

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 5/72

INFORMATION HIDING

Inf ormation hiding is the principle of 

segregation of design decisions in a computer

program that are most likely to change, thus

protecting other parts of the program from

modifications.

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 6/72

Example of inf ormation hiding

Information hiding serves as an effective criterion fordividing any piece of equipment, software or hardware,into modules of functionality.

For instance a car is a complex piece of equipment. In order to make the design, manufacturing, and

maintenance of a car reasonable, the complex piece of equipment is divided into modules with particularinterfaces hiding design decisions.

By designing a car in this fashion, a car manufacturercan also offer various options while still having avehicle which is economical to manufacture.

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 7/72

Encapsulation

object contain both data structure and set of operation used to manipulate it

Information hidingseparate external aspect of an object from its

internal details which are hidden from outside

allows internal detail of an object to be

changed without affecting application that used itprovide external detail remain same

provides data independence

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 8/72

Methods and messages

METHOD

Define behavior of object as a set of 

encapsulated function MESSAGE

Request from one object to another asking

second object to execute one of the method

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 9/72

Data Abstraction

Abstract

We can't access the private data of a class directly

class A

{int x;

};

main()

{A a1;

a1.x = 100; Error

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 10/72

Data Encapsulation

private datas and public functions binded together like a capsule

class A

{

int x;

public:

int y;

void getA()

{

x = 199;

cout << "X = " << x << endl;

}

};

main()

{

A a1;

a1.y = 200;

a1.getA();

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 11/72

Structure Oriented Programs

We write the programs for aimed Results

eg: C, Fortran, Cobol etc

Object Oriented Program

We write the programs for the DeclaredData

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 12/72

Types of Programming in Java

1. OOPS

Client/Server Applications

Using GUI Tools

2. NetworkProtocals (set of Rules and Regulations)

FTP

HTTP

3. InternetServer Side Programs

(servlet,jsp,ejb etc)

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 13/72

OOPS concepts not in Java

Operator Overloading

Multiple Inheritance(upto Interface)Hierarhical Inheritance

Hybrid Inheritance

TemplatesPointers

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 14/72

OOPS in Java

1. Class

2. Object3. Data Abstraction & Encapsulation

4. Polymorphism

5. Inheritance6. Exception

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 15/72

Class

Collection of private data/variables and public functions/methods

Variable

To store a value

Array

To store more than one value into a variable //int month[]; month = new int[12] ;Structure

To store more than one data type into a variable

struct Stud

{

int no;

char na;

};

Object

Allocates Memory

Its a variable

Used to access the class data and functions

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 16/72

Java

Collection of Packages(Directories)

Package

Collection of Classes and Interfaces

ClassCollection of private data and public functions

Interface

Collection of public final variables and public abstract methods

JVM

Java Virtual Machine

Used to convert the sourcecode to Machinecode

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 17/72

Structure of java Prg:

[package <Pname>;]

[import <Pname>.*;]

class <Cname>

{

}

class <Cname>

{

}

.

.

.

class <Cname>

{

public static void main(String args[])

{

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 18/72

The keyword public denotes that a method can becalled from code in other classes, or that a classmay be used by classes outside the class hierarchy.

The keyword static in front of a method indicates astatic method, which is associated only with theclass and not with any specific instance of thatclass. Only static methods can be invoked without a

reference to an object. Static methods cannotaccess any method variables that are not static.

The keyword void indicates that the main methoddoes not return any value to the caller.

T

he method name "main" is not a keyword in theJava language. It is simply the name of the methodthe Java launcher calls to pass control to theprogram.

String arg[]- command line arguments

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 19/72

Printing statement

print

Used to print the content and place the cursor insame line

syn:System.out.print([String]);

println

Used to print the content and place the cursor in

next linesyn:

System.out.println([String]);

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 20/72

class sample

{

public static void main(String args[]){

System.out.println(hello);

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 21/72

To Compile

 javac clsname.java

(if there is no compile time errors in our prg then)clsname.class

T

o Run java clsname

We get the output

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 22/72

Class in javaclass box{

double width;

double height;

double depth;

}

class demo

{

public static void main(String args[])

{

box mybox =new box();

double vol;

mybox. Width=10;

mybox height=20;

mybox height=30;

Vol =mybox. Width*mybox height*mybox height

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 23/72

Class variable = new class()

Box mybox; ---------------->Mybox = new box()----

null

mybox

Width

Height

Depth

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 24/72

class box{

double width;

double height;

double depth;

}class demo

{

public static void main(String args[])

{

box mybox1 =new box();

box mybox 2=new box();

double vol;

mybox1. Width=10;

mybox1height=20;

mybox1 height=30;

mybox2. Width=10;

mybox2 height=20;mybox2 height=30;

Vol =mybox1. Width*mybox1 height*mybox1 height;

Vol =mybox2 Width*mybox2 height*mybox2 height;

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 25/72

class box{

double width;

double height;

double depth;

void volume(){

System.out.println(width*height*depth);

}

}

class demo

{

public static void main(String args[])

{

box mybox1 =new box();

box mybox2=new box();

double vol;

mybox1 Width=10;

mybox1height=20;

mybox1 height=30;

mybox2 Width=10;mybox2 height=20;

mybox2 height=30;

mybox1.volume();

mybox2.volume();

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 26/72

class box{

double width;

double height;

double depth;

double volume(){

return (width*height*depth);}

}

class demo

{

public static void main(String args[])

{

box mybox1 =new box();

box mybox2=new box();

double vol;

mybox1 Width=10;

mybox1height=20;

mybox1 height=30;

mybox2 Width=10;

mybox2 height=20;

mybox2 height=30;

vol = mybox1.volume();

system.out.println(volume is+vol);

vol = mybox2.volume();

system.out.println(volume is+vol);

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 27/72

METHODS THAT TAKES PARAMETER

class box{

double width;

double height;

double depth;

double volume(){

return (width*height*depth);

}

void setdimention( double w, double h,double d){

width =w;

height =h;

depth =d;

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 28/72

class demo

{

public static void main(String args[]){

box mybox1 =new box();

box mybox2=new box();

double vol;

mybox1.setdimention(10,20,15);

mybox2.setdimention(10,20,15);

mybox1 Width=10;

mybox1height=20;

mybox1 height=30;

mybox2 Width=10;mybox2 height=20;

mybox2 height=30;

vol = mybox1.volume();

system.out.println(³volume is´+vol);

vol = mybox2.volume();

system.out.println(³volume is´+vol);

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 29/72

Superclass and subclass

Superclass or parent class is the one from which

another class inherits attributes and behavior

Sub class or child class is the class that inherits

attribute and behavior from Superclass

Example air ticket can be of two types

confirmed and requested it has various attribute

such as flight number, date, time, destination both

types of air ticket inherits these common attributes

the conformed ticket will have seat number while

requested ticket will have a status attribute

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 30/72

Class: AIRTECKET

Attributes:FLIGHT NAME

DATE

TIME

DESTINATION

Class:

REQUESTEDTICKET

Attributes:

FLIGHT NAME

DATE

TIME

DESTINATION

Class: 

CONFIRMTICKET

Attributes:

FLIGHT NAME

DATE

TIME

DESTINATION

Own Attribute:

SEATNUMBER

Own Attribute:

STATUS

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 31/72

Relation ship between class

Class and object are related to and interact

with each other

In oop object performs action in response tomessage from other object defining the

receiving object behaviour

Kind of 

Is a

Part of 

Has a

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 32/72

Kind of Relationship

Sub class is a type of superclass

Confirmed ticket Ticket

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 33/72

Is a Relationship

Sub class is a type of superclass

Ticket class is superclass confirm class is subclass conform ticket to newyork is an object of 

confirm ticket class

Confirmed ticket

TicketTicket for

NEWYORK

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 34/72

Part of Relationship

When class is element of another class it is part

of class

address student

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 35/72

Has a Relationship

Reverse of part of relation ship

Student details address

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 36/72

Access specifiers

Public

Private

Protected Friendly or package

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 37/72

modifiers

Static

Final

Abstract Native

Modifiers determine how the data members andmethods are used in other class and objects

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 38/72

Diff between access specifier and

modifier

Access specifier defines the accessibility of 

data member in the class

Modifier determines how these methods areused and modified by other class

Static variable, methods, innerclass

Final variable, methods, class

Abstract methods, class

Native methods

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 39/72

Passing arguments

Passing arguments by value

Passing arguments by reference

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 40/72

Formal parameter 

 Actual parameter 

Value 1

Value 2

0X123456

0X123777

New

memory

PASSING ARGUMENTS AS VALUE

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 41/72

When arguments of primitive data type such

as int float are passed to method they arepassed as value

When arguments are passed by value a copy

of value of actual arguments is passed to

formal arguments of called method which is

maintained in different memory

When called method changes the value of the

arguments the changes is not reflected in theactual arguments

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 42/72

Class passbyvalue

{

void calling()

{

int value =5;S.o.pln(The value of variable is:+value);

called(value);//actual parameter

S.o.pln(The value of variable af ter change in called method is:+value);

}

void called( int passedvalue ) //f ormal parameter

{

passedvalue = 10;

}

}

Class passbyvaluetest

{

public static void main(String args[]){

passbyvalue pbv = new passbyvalue();

pbv.calling();

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 43/72

Formal parameter 

 Actual parameter 

Value 1

Value 2

0X123456

0X123456

Same

memory

PASSING ARGUMENTS AS REFERENCE

Cl f bj t

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 44/72

Class ref object

{

int val;

}

Class passbyref erence

{void calling()

{

rerobject ro new f erobject();

ro.val = 5;

S.o.pln(The value of variable is:+ ro.val);

called(ro);//actual parameter

S.o.pln(The value of variable af ter change in called method is:+ro.val);

}

void called( ref object ro ) //f ormal parameter

{

ro.val = 10;

}

}

Class passbyvalueref erence

{

public static void main(String args[])

{

passbyref erence pbr = new passbyref erence();

pbr.calling();

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 45/72

Creating Your Own Packages

Each package class must be stored in a file in an

appropriately named directory

The source code file for each package class must contain a

package statement as its first non-commented statement

package pack age_name;

Several packages can be stored in the same directory

Classes in different directories cannot be part of the same

package

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 46/72

class student{

int regno;

string name;

public string (int reg, string na)

{

regno = reg;name = na;

}

public void print()

{

S.o.pln(³\n register number:´+regno);

S.o.pln(³\n name:´ +name);}

}

class diploma

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 47/72

class diploma

{

int regno;

String disipline;

String year;

public diploma(int reg, String dis, String ye){

regno = reg;

disipline= dis;

year = ye;

}

public void print()

{ S.o.pln(³\n course:´+disipline);

S.o.pln(³\n year:´ +year);

}

}

class demo

{

public static void main(String args[]){

student s1 =new student(1,´kumar´);

Diploma d1 = new diploma(1,´computer´,´I year´);

s1.print();

d1.print();

}

}

P k ll

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 48/72

Package college;

public class student

{

int regno;

string name;public string (int reg, string na)

{

regno = reg;

name = na;

}public void print()

{

S.o.pln(\n register number:+regno);

S.o.pln(\n name: +name);

}}

Package course;

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 49/72

Package course;

public class diploma

{

int regno;

String disipline;String year;

public diploma(int reg, String dis, String ye)

{

regno = reg;

disipline= dis;year = ye;

}

public void print()

{

S.o.pln(\n register number:+disipline);

S.o.pln(\n name: +year);

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 50/72

import college.*;

import course.*;class demo

{

public static void main(String args[])

{

student s1 =new student(1,kumar);Diploma d1 = new diploma(1,computer,I year);

s1.print();

d1.print();

}

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 51/72

Inheritance

Allows programmers to customize a class for a

specific purpose, without actually modifying the

original class (the superclass) The derived class (subclass) is allowed to add

methods or redefine them

The subclass can add variables, but cannot

redefine them

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 52/72

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 53/72

Inheritance Example

Class C is a subclass of class B (its superclass) if itsdeclaration has the form

class C extends B {«

}

The subclass is a s peci al izat ion of the superclass

The superclass is a gener al izat ion of the subclass

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 54/72

Types of inheritance

Single inheritance

Multilevel inheritance

Interface (multiple inheritance in c++)

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 55/72

Single inheritance

Sub class is derived from single superclass

AIRTICKET

CONFIRMED REQUEST

SUPERCLASS

SUBCLASS SUBCLASS

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 56/72

EXAMPLE

Class A

{

}Class B extends A

{

}Class C extends A

{

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 57/72

Inheritance in C++

Single inheritance

Multiple inheritance

Hierarchical inheritance Multilevel inheritance

Hybrid inheritance

Multipath inheritance

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 58/72

A

B C

B A

B

A

D

A

B

A

B

A

C D

C

B C C

D

Single inheritance Multiple inheritance Hierarchical inheritance

Multilevel inheritanceHybrid inheritance Multipath inheritance

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 59/72

Single Inheritance

Here we have only two classes. One is

called as parent class (Base) and other iscalled as child class (Derived).

Class B

Child or Derived Class

Class A

Parent or Base Class

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 60/72

syntax

class derivedclassname : visibilitymobe baseclassname

{

};

Visibility mode can be

private

public

protected

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 61/72

Visibility of inherited members

Base class visibility derived class visibility

Public derived

DCVPrivate derived

Private Not Not

Protected Protected Private

Public Public private

//single inheritance PUBLIC

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 62/72

g

class B

{

int a;

public:

int b;void getab();

void showa();

int geta();

};

Class D : public B

{int c;

Public:

Void mul();

Void display();

};

Main(){

D d;

d.getab();

d.mul();

d.display();

d.b=20;d.mul d.dis la

void B::getab()

{

a =5,b=10;

}

int B::geta()

{

return a;

}

void::showa()

{

cout<< a;}

void D::mul()

{

c =b *geta();

} void D::display()

{

cout<<b<<c;

}

Output

B=10C=50

A=5

B=20

C=100

//single inheritance private

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 63/72

g p

class B

{

int a;

public:

int b;void getab();

void showa();

int geta();

};

Class D : private B

{int c;

Public:

Void mul();

Void display();

};

int main(){

D d;

//d.getab();

d.mul();

d.display();

//d.b=20;

//not possible b is privated.mul d.dis la

void B::getab()

{

a =5,b=10;

}

int B::geta()

{

return a;

}

void::showa()

{

cout<< a;}

void D::mul()

{

getab();

c =b *geta();

}void D::display()

{

showa();

cout<<b<<c;

}

Output

510

50

5

10

50

 

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 64/72

PRIVATE

PROTECTED

PUBLIC

PRIVATE

PROTECTED

PUBLIC

PRIVATE

PROTECTED

PUBLIC

PRIVATE

PROTECTED

PUBLIC

CLASS D1:PUBLIC BCLASS D2:PRIVATE B

CLASS X:PUBLIC D1:PUBLIC D2

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 65/72

Multilevel Inheritance

Class A

Parent class for B & C

Class B

Child class for A

Parent class for C

Class C

Child class for A & B

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 66/72

Example for Multilevel

Inheritance

Student Class

Roll No

GetNum ( )

PutNum ( )

Test Class

Sub1, Sub2

GetMarks ( )

PutMarks ( )

Result Class

total

Display ( )

class studentvo s u en ::ge num er n a

{

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 67/72

class student

{

protected:

int rollnumber;

public:

void getnumber(int a);

void displaynumber();

};

class test:public student

{

protected:float sub1,sub2;

public:

void getmarks(float x,float y);

void displaymarks();

};

class result:public test

{

float total;

public:

void display();

};

{

rollnumber =a;

}

int main(){

result student1;

student1.getnumber(100);

student1.getmarks(50,50);

student1.display();

getch();

void result::display(){

total =sub1+sub2;

displaynumber();

displaymarks();

cout<<total;

}

void student::displaynumber()

{

cout<<rollnumber;

}

void test::getmarks(float x,float y)

{

sub1=x;

sub2=y;

}void test::displaymarks()

{

cout<<sub1<<sub2;

}

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 68/72

Multiple inheritance

Class N

Parent class2

Base Class2

Class M

Parent class1

Base class1

Class P

Child class

Derived Class

class Avoid A::geta(int x)

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 69/72

{

protected:

int a;

public:

void geta(int);

};

class B

{

protected:

int b;

public:

void getb(int);};

class C:public A,public B

{

int c;

public:

void add();

void display();

};

int main(){

C c;

c.geta(10);

c.getb(5);

c.add();

c.display();

getch();

}

void A::geta(int x)

{

a=x;

}

void B::getb(int y)

{

b=y;

}void C::add()

{

c=a+b;

}

void C:: display(){

cout<<a<<"\n";

cout<<b<<"\n";

cout<<c;}

C

Add()

Display()

a

Geta()

b

Getb()

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 70/72

Hybrid Inheritance

Class A

Parent class for B & C

Class B

Child class for A

Parent class for C

Class C

Child class for A & B

Class D

Parent class for C

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 71/72

Example for Hybrid Inheritance

Student Class

Roll No

GetNum ( )

PutNum ( )

Test Class

Sub1, Sub2

GetMarks ( )

PutMarks ( )

Result Class

Total=sub1+sub2+

score

Display ( )

Sports Class

Score

GetScore ( )

PutScore ( )

class student

{

void student::getnumber(int a)

{

8/7/2019 encapsulation and information hiding

http://slidepdf.com/reader/full/encapsulation-and-information-hiding 72/72

{

protected:

int rollnumber;

public:

void getnumber(int a);

void displaynumber();

};

class test:public student

{

protected:

float sub1,sub2;

public:

void getmarks(float x,float y);

void displaymarks();

};

class result:public test

{

float total;

public:

void display();

};

{

rollnumber =a;

}

int main()

{

result student1;

student1.getnumber(100);

student1.getmarks(50,50);

student1.display();

getch();

}

void result::display(){

total =sub1+sub2;

displaynumber();

displaymarks();

cout<<total;

}

void student::displaynumber()

{

cout<<rollnumber;

}

void test::getmarks(float x,float y)

{

sub1=x;

sub2=y;

}

void test::displaymarks()

{

cout<<sub1<<sub2;

}

class sports

{

protected:

float score;

public:

void getscore(float s);

void displayscore();

};

void sports :: getscore(float s){

score =s;

}

void sports::displayscore()

{

cout<<score;

}


Recommended