Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Chapter 6

Object-Oriented Programming
Object Oriented Programming in C++ - GeeksforGeeks
Outlines

Introduction
Basic components of oop
Principles of oop
Introduction
Object-oriented programming aims to implement real-world entities like inheritance,
hiding, polymorphism, etc. in programming.
The main aim of OOP is to bind together the data and the functions that operate on
them so that no other part of the code can access this data except that function.
Object-oriented programming is about creating objects that contain both data and
functions
Procedural programming is about writing procedures or functions that perform
operations on the data
Advantages of Object-oriented programming (OOP):
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the code “Don't Repeat Yourself”
OOP makes the code easier to maintain, modify and debug
OOP create full reusable applications with less code and shorter development
time
• There are some basic concepts that act as the building blocks of OOPs
i.e.
1.Class
2.Objects
3.Encapsulation
4.Abstraction
5.Polymorphism
6.Inheritance
-
Class and Object

• A Class is a user-defined data type that has data members and member functions.
• A class is a collection of a fixed number of components.
• Data members are the data variables and member functions are the functions
used to manipulate these variables together these data members and member
functions define the properties and behavior of the objects in a Class.
• Object
• An Object is an identifiable entity with some characteristics and behavior. An
Object is an instance of a Class. When a class is defined, no memory is allocated
but when it is instantiated (i.e. an object is created) memory is allocated.
• Class is a set of similar objects
• Class is a blueprint to create objects of that class.
• Class is defined with Class keyword
• Object is an instance of a class
The general syntax for defining a class is:
 class classIdentifier { classMembersList };
Example, consider a class called Student has student objects
 student object has attributes like
 ID
 Name
 Gender
 GPA
student object has behaviors like
Register()
viewGrade ()
AddCourse ()
DropCourse ()
Withdraw ()
Access specifiers

• In C++, there are three access specifiers:


• public - members are accessible from outside the class
• private - members cannot be accessed outside the class
• protected - members cannot be accessed outside the class,
however, they can be accessed in (subclasses) inherited classes.
Methods
Methods are functions that belongs to the class.
There are two ways to define functions that belongs to a class:
Inside class definition
Outside class definition
To define a function outside the class definition,
you have to declare it inside the class and then define it outside of the
class.
This is done by specifying the name of the class, followed the scope
resolution :: operator, followed by the name of the function:
Constructors
A constructor is a special method that is automatically called when an
object of a class is created.
To create a constructor, use the same name as the class, followed by
parentheses ():
The constructor should be always public, and it does not have any
return value.
Constructors can also take parameters (just like regular functions),
which can be useful for setting initial values for attributes.
Constructors have the following properties:
The name of a constructor is the same as the name of the class.
 A constructor is a function and it has no type. That is, it is neither a value-
returning function nor a void function.
 A class can have more than one constructor. However, all constructors of a class
have the same name.
If a class has more than one constructor, the constructors must have different
formal parameter lists. That is, either they have a different number of formal
parameters or, if the number of formal parameters is the same, then the data type
of the formal parameters, in the order you list, must differ in at least one position.
In other words, like function overloading, a constructor’s name is overloaded.
 Constructors execute automatically when a class object is declared and enters its
scope. Because they have no types, they cannot be called like other functions.
Which constructor executes depends on the types of values passed to the class
object when the class object is declared.
Principles of OOP
• There are four principles of object-oriented programming
• Inheritance
• Encapsulation
• Polymorphism
• Abstraction
Encapsulation
Encapsulation refers to the bundling of related fields and methods
together.
This makes sensitive data hidden from users
To achieve this, you should declare class attributes as private
If you want others to read or modify the value of a private member,
you can provide public get and set methods.
Encapsulation ensures better control of data, because you can change
one part of the code without affecting other parts
Inheritance
It is possible to inherit attributes and methods from one class to another.
A new class (NC) can be created by absorbing the methods and variables of an
existing class
NC then adds its own methods to enhance its capabilities
Objects of derived class are objects of base class but not vice versa
Every derived-class object is an object of its base class
There are two class in inheritance (sub-class and super class)
derived class (child) - the class that inherits from another class
base class (parent) - the class being inherited from
To inherit from a class, use the : symbol
Inheritance enable code reusability
Reading assignment Types of inheritance
Polymorphism
• Polymorphism means "many forms“
• It occurs when we have many classes that are related to each other by
inheritance.
• Polymorphism uses inherited methods to perform different tasks.
• This allows us to perform a single action in different ways.
Abstraction
Data abstraction refers to providing only essential information to the
outside world and hiding their background details
A technique that separates interface and implementation.
Examples
1. If you have a person class which contains id, sex, country and name
as an attributes and display() as a method for displaying recorded
information. Please try the following:-
Give the value id=1345 ,sex =M country = Ethiopia and name =ABCD
Accept the inputs from key board
Display the information's of 5 persons
2. Inherit student class from person which contains CGPA (AM private)in
addition to person class and displayInfo() as method .
Reading assignment
Types of inheritance
Differentiate instance and static class, variable, method ….etc
Other access modifiers(AC) like internal, new …etc around 10 AM
Over all concepts about exceptional handling

You might also like