5. Real World Entities – More Classes
www.SunilOS.com 5
:Automobile
-color :String
-speed:int
-make:String
+$NO_OF_GEARS
+getColor():String
+setColor()
+getMake():String
+setMake()
+break()
+changeGear()
+accelerator()
+getSpeed():int
:Person
-name:String
-dob : Date
-address:String
+$AVG_AGE
+getName():String
+setName()
+getAdress():String
+setAddress()
+getDob (): Date
+setDob ()
+getAge() : int
:Account
-number:String
-accountType : String
-balance:double
+getNumber():String
+setNumber()
+getAccountType():String
+setAccountType()
+deposit ()
+withdrawal ()
+getBalance():double
+fundTransfer()
+payBill()
6. www.SunilOS.com 6
Constructor
class Shape {
public Shape(){
cout<<“This is default
constructor”;
}
};
Shape s;
Constructor is just like a method.
It does not have return type.
Its name is same as Class name.
It is called at the time of object
calling.
Constructors are used to initialize
instance/class variables.
A class may have multiple
constructors with different number
of parameters.
7. www.SunilOS.com 7
Multiple Constructors
One class may have more than one constructors.
Multiple constructors are used to initialize different
sets of class attributes.
When a class has more than one constructors, it is
called Constructor Overloading.
Constructors those receive parameters are called
Parameterized Constructors.
9. Default Constructor
Default constructor does not receive any parameter.
o public Shape(){ .. }
If User does not define any constructor then Default
Constructor will be created by Compiler.
But if user defines single or multiple constructors then
default constructor will not be generated by Compiler.
www.SunilOS.com 9
10. Destructor
It is inverse of constructor function.
It is used to deallocate the memory or
release the resources like closing files.
It is called when object is destroyed.
It has a same name as class name used
tilde(~) as prefix.
~class_name(){…….}
www.SunilOS.com 10
11. Copy Constructor
It is a special type of constructor .
It is used to create new object as a copy of
existing object.
It is a standard way to copy an object.
It is used to initialize one object from another
object of the same type.
www.SunilOS.com 11
13. Operator Overloading
Built-in operators can be redefined &
overloaded.
Overloaded operators are functions with special
names.
It is used to perform operations on user defined
data type.
+ operator overloaded on two integer values to
perform addition.
+ operator overloaded on two string values to
perform concatenation.
www.SunilOS.com 13
17. Scope Resolution Operator ::
It is used to qualify hidden name.
When local variable and global variable are
having same name, local variable gets the
priority.
C++ allows flexibility of accessing both the
variables through a scope resolution
operator.
www.SunilOS.com 17
18. int a = 10;
void main(){
o int a = 15;
o cout<<a<<::a;
o ::a=20;
o Cout<<a<<::a;
}
Output :15 10
15 20
www.SunilOS.com 18
19. OOP Key Concepts
Encapsulation:
o Creates Expert Classes.
Inheritance:
o Creates Specialized Classes.
Polymorphism:
o Provides Dynamic behaviour at Runtime.
www.SunilOS.com 19
20. www.SunilOS.com 20
Encapsulation
Gathering all related methods and attributes in a
Class is called encapsulation.
Often, for practical reasons, an object may wish
to expose some of its variables or hide some of
its methods.
Access Levels:
Modifier Class Subclass Packag
e
World
private X
protected X X X
public X X X X
21. Friend Function
There is a mechanism in C++ to access
private & protected members of a class.
This mechanism is known as friend function
or friend class.
We have to use friend keyword in prefix of
function name.
Declaration should be inside the class body
starting with keyword friend.
www.SunilOS.com 21
22. class Distance {
o private:
o int meter;
o public:
o Distance(): meter(0){ }
o friend int func(Distance); //friend function
};
int func(Distance d) {//function definition
o d.meter=5; //accessing private data from non-member function
o return d.meter;
} int main() {
o Distance D;
o cout<<"Distace: "<<func(D);
o return 0;
}
www.SunilOS.com 22
26. Polymorphism
One thing with multiple forms.
Two types of polymorphism
Static Polymorphism(Overloading)
Dynamic Polymorphism(Overriding)
Ways to Provide polymorphism:
o Through Interfaces.
o Method Overriding.
o Method Overloading.
www.SunilOS.com 26
28. www.SunilOS.com 28
Abstract Class
What code can be written in Shape.area() method?
o Nothing, area() method is defined by child classes. It should have
only declaration.
Is Shape a concrete class?
o NO, Rectangle, Circle and Triangle are concrete classes.
If it has only area declaration then
o Class will be abstract.
Benefit?
o Parent will enforce child to implement area() method.
o Child has to mandatorily define (implement) area method.
o This will achieve polymorphism.
30. www.SunilOS.com 30
Interface / Virtual function
A class is made abstract by declaring at least one
of its functions as pure virtual function
A pure virtual function is specified by placing "= 0"
in its declaration
public:
virtual double area() = 0;
33. Data Abstraction ( Cont. )
Data abstraction is the way to create complex data
types and exposing only meaningful operations to
interact with data type, whereas hiding all the
implementation details from outside world.
Data Abstraction is a process of hiding the
implementation details and showing only the
functionality.
Data Abstraction in java is achieved by interfaces
and abstract classes.
www.SunilOS.com 33
35. Data Hiding ( Cont. )
Data Hiding is an aspect of Object Oriented
Programming (OOP) that allows developers
to protect private data and hide
implementation details.
Developers can hide class members from
other classes. Access of class members
can be restricted or hide with the help of
access modifiers.
www.SunilOS.com 35
36. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 36