0% found this document useful (0 votes)
141 views33 pages

Chapter 3 - OOP in C++

This document covers the basics of templates and object-oriented programming (OOP) in C++. It discusses key OOP concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with their implementation in C++. The document provides examples and explanations of template functions, constructors, destructors, and the significance of abstract classes.

Uploaded by

hadytarabay12
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)
141 views33 pages

Chapter 3 - OOP in C++

This document covers the basics of templates and object-oriented programming (OOP) in C++. It discusses key OOP concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with their implementation in C++. The document provides examples and explanations of template functions, constructors, destructors, and the significance of abstract classes.

Uploaded by

hadytarabay12
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/ 33

Data Structures and Algorithms

Chapter 3 – Templates
Basics of OOP in C++

Dr. Georges Badr

1
OUTLINE

•Templates (function and class)

•Basics of object-oriented programming in C++:


• Class & Object
• Encapsulation
• Inheritance
• Abstraction
• methods overriding

2
Template functions
Templates provide the capability to parameterize types in functions and classes.

You can define functions or classes with generic types that can be substituted
for concrete types by the compiler.

3
Template functions

Overloaded functions: same


name different type/number
of params

Template function that works


with any datatype

4
Template functions

5
Template functions

A generic function to swap 2 elements:

Syntax error
v1 is an int
d2 is a double

6
What is OOP?

•OOP stands for Object-Oriented Programming.

•Procedural programming is about writing procedures or functions that perform


operations on the data, while object-oriented programming is about creating
objects that contain both data and functions.

•Object-oriented programming has several advantages over procedural


programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to
maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and shorter
development time
7
Classes and Objects

•A class is a template for objects


• It is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class
• Ex: Car is the class and wheels, speed limits, mileage are their properties and
member functions can apply brakes, increase speed etc.

•An object is an instance of a class.

•When the individual objects are created, they inherit all the
variables and functions from the class.

8
Class
Point
Consider this UML - name: char
diagram for the class - x: double
- y: double
Point
+ Point (): Default constructor
+ display() : void //displays the point as name (x, y). i.e: A (5, 5)
+ scale(double): void //multiplies the coordinates by the value
+ move(double): void //adds the value to the coordinates
+ offset(double): void //translates the coordinates to the value
add getters and setters

9
Class
Point
Consider this UML - name: char
diagram for the class - x: double
- y: double
Point
+ Point (): Default constructor
+ display() : void //displays the point as name (x, y). i.e: A (5, 5)
+ scale(double): void //multiplies the coordinates by the value
+ move(double): void //adds the value to the coordinates
+ offset(double): void //translates the coordinates to the value
add getters and setters

10
Class
Note that methods
can be implemented
inline (directly in the
class)

11
Constructors
Default constructor with default parameters
A constructor is a special type
of methods to create an object
◦ No return type
◦ Same name as the class
name

Overloaded constructor with 3 parameters


Default constructor with no parameters

Constructor with default parameters and initializers

Default constructor with initializers


Overloaded constructor and initializers

12
Destructor
•A destructor is a member function that is invoked automatically when the object
goes out of scope or is explicitly destroyed by a call to delete.

•A destructor has the same name as the class, preceded by a tilde (~).

•For example, the destructor for class Point is declared: ~Point().

•If you do not define a destructor, the compiler will provide a default one; for
many classes this is sufficient.

•You only need to define a custom destructor when the class stores resources that
need to be released, or pointers that own a memory location.
13
Destructor

•Do not accept arguments.


•Do not return a value (or void).
•Destructors are called when one of the following events occurs:
• A local (automatic) object with block scope goes out of scope.
• An object allocated using the new operator is explicitly deallocated using delete (will be covered lately)
• The lifetime of a temporary object ends.
• A program ends and there are global or static variables/objects.
• The destructor is explicitly called using the destructor function's fully qualified name.
• Example:
~Point(){
cout << “Object destroyed”;
}//destructor

14
Data access

Error
Members are private
No access outside the class

15
Data access

No error
Not recommended to have public
members

16
Encapsulation

Binding attributes to functions.


To protect the data members, we set them
as private and then we create 3 functions:
◦ Accessor or getter to read the value
◦ Mutator or setter to change the value

17
Encapsulation
Member of the class can be objects.
A circle is defined by its center (which is a
point) and a double radius

output

18
Template class
Let’s consider a class MemberCard that has 2 members (key and value)
we want the class to support values of any data type?
◦ int, double, char, string, etc.

We can define a template or generic class

19
Template class

Members have char type

Members have int type

Members have string type

Members have double type

20
Inheritance
 Inheritance is a mechanism by which a class can be derived from another class.

 The derived class inherits all the members of the base class, such as data
members and member functions.

 The base class is also called the superclass or parent class, and the derived class
is also called the subclass or child class.

 To declare a derived class in C++, you use the syntax:


class DerivedClass : AccessSpecifier BaseClass {
// body of the derived class
};

 The AccessSpecifier can be either public, protected, or private. 21


Inheritance accessibility
 public, protected, and private inheritance have the following features:

• public inheritance makes public members of the base class public in the derived
class, and the protected members of the base class remain protected in the
derived class.
• protected inheritance makes the public and protected members of the base
class protected in the derived class.
• private inheritance makes the public and protected members of the base class
private in the derived class.

 Note: private members of the base class are inaccessible to the derived class

22
Inheritance
 We will take the example of a Person, Student and Instructor.
•They all have an ID and a Name
•Student has a GPA
•Instructor has a salary

Person

Student Instructor

23
24
25
26
27
Polymorphism
• Polymorphism is the ability of objects to take on different forms.
• C++ supports two types of polymorphism: compile-time polymorphism and
runtime polymorphism.
• Runtime polymorphism is achieved through virtual functions and dynamic
binding. A virtual function is a member function of a base class that is declared
with the keyword virtual and can be overridden in a derived class.
• Dynamic binding is the process of determining at runtime which version of a
virtual function to call based on the type of object pointed to by a base class
pointer.

virtual returnType functionName(parameters) {


// function body
}

28
Polymorphism
 To override a virtual function in a derived class, you use the same function
signature and the keyword override

returnType functionName(parameters) override {


// function body
}

29
Polymorphism
• An abstract function is a virtual function that is declared in a base class but has no
implementation.
• An abstract function is declared using the syntax:
virtual returnType functionName(parameters) = 0;
• The = 0 syntax indicates that the function has no implementation and is therefore called a pure
virtual function.
• A class that contains one or more pure virtual functions is called an abstract class, and
cannot be instantiated on its own
• This function must be overridden in the derived or children classes.
• We can no longer instantiate the base class, which means we cannot create objects of type Base
class.
• Instead, abstract classes are meant to be used as base classes for other classes that provide
concrete implementations of the pure virtual functions.

30
Polymorphism

• Polymorphism is useful here because we can write functions or


arrays of type the base class and then pass parameters or fill the
array with objects of types of derived classes.

31
Example
• A fulltime employee has an id, name and a salary. We would like to compute the salary for
any full-timer.
• A part-time employee has an id, name, number of hours worked and rate per hour. We would
like to compute the salary for any part-timer.
• To model this example, we create a base class Employee that has the id and name attributes, and
we define an abstract function compute salary which we override in the fulltime and parttime
classes.
• We can no longer create employee objects. Employee

FullTime PartTime

32
or pure virtual

33

You might also like