0% found this document useful (0 votes)
11 views55 pages

C++ OOP Concepts and Examples

The document outlines a C++ course focused on Object Oriented Programming (OOP) concepts, including classes, objects, constructors, destructors, access specifiers, inheritance, function overloading, and polymorphism. It emphasizes the importance of organizing code using namespaces and header files. Key topics include the definition and usage of classes and objects, as well as the role of pointers and references in C++.

Uploaded by

joshi.vishnu1994
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)
11 views55 pages

C++ OOP Concepts and Examples

The document outlines a C++ course focused on Object Oriented Programming (OOP) concepts, including classes, objects, constructors, destructors, access specifiers, inheritance, function overloading, and polymorphism. It emphasizes the importance of organizing code using namespaces and header files. Key topics include the definition and usage of classes and objects, as well as the role of pointers and references in C++.

Uploaded by

joshi.vishnu1994
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

C++ course

OOP concepts, pointers and references

Vishnu Joshi
joshivis@[Link]
Oregon State University
Object Oriented Programming
Object Oriented Programming
Classes and Objects
Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism What are Classes?
Abstraction and
Namespace

Pointers and
references

4/23
Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism What are Classes?
Abstraction and
Namespace Class is a blueprint for your data and functions manipulating these data
Pointers and
references

4/23
Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism What are Classes?
Abstraction and
Namespace Class is a blueprint for your data and functions manipulating these data
Pointers and
references
What are objects?

4/23
Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism What are Classes?
Abstraction and
Namespace Class is a blueprint for your data and functions manipulating these data
Pointers and
references
What are objects?
Objects are just instances of the classes

4/23
Object Oriented Programming
Example of Classes and Objects
Example of Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism Think of a simple 2D robot.
Abstraction and
Namespace

Pointers and
references

6/23
Example of Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism Think of a simple 2D robot. It can move 3 ways or has 3 DoF.
Abstraction and
Namespace

Pointers and
references

6/23
Example of Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism Think of a simple 2D robot. It can move 3 ways or has 3 DoF.
Abstraction and
Namespace Translation along x and y axes. Rotation along the z axis.
Pointers and
references

6/23
Example of Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism Think of a simple 2D robot. It can move 3 ways or has 3 DoF.
Abstraction and
Namespace Translation along x and y axes. Rotation along the z axis.
Pointers and
references
So how many variables should we have?

6/23
Example of Classes and Objects
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator
polymorphism Think of a simple 2D robot. It can move 3 ways or has 3 DoF.
Abstraction and
Namespace Translation along x and y axes. Rotation along the z axis.
Pointers and
references
So how many variables should we have?
x , y and θ

6/23
Object Oriented Programming
Constructors and Destructors
Object Oriented
Constructors and Destructors
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
What are Constructors?
operator
polymorphism
Abstraction and
Namespace

Pointers and
references

8/23
Object Oriented
Constructors and Destructors
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
What are Constructors?
operator
polymorphism
Abstraction and
Function that is executed automatically executed when an object is
Namespace
created.
Pointers and
references

8/23
Object Oriented
Constructors and Destructors
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
What are Constructors?
operator
polymorphism
Abstraction and
Function that is executed automatically executed when an object is
Namespace
created.
Pointers and
references

What are destructors?

8/23
Object Oriented
Constructors and Destructors
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
What are Constructors?
operator
polymorphism
Abstraction and
Function that is executed automatically executed when an object is
Namespace
created.
Pointers and
references

What are destructors?


Function that is executed automatically executed when an object is
deleted or goes out of scope

8/23
Object Oriented Programming
Access Specifiers
Access Specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
What are access specifiers?
Function overloading
Virtual function and
operator
polymorphism
Abstraction and
Namespace

Pointers and
references

10/23
Access Specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
What are access specifiers?
Function overloading
Virtual function and
They help in assigning the accessibility to the items of a class
operator
polymorphism
Abstraction and
Namespace

Pointers and
references

10/23
Access Specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
What are access specifiers?
Function overloading
Virtual function and
They help in assigning the accessibility to the items of a class
operator
polymorphism
Abstraction and
Namespace
How many access specifiers are there?
Pointers and
references

10/23
Access Specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
What are access specifiers?
Function overloading
Virtual function and
They help in assigning the accessibility to the items of a class
operator
polymorphism
Abstraction and
Namespace
How many access specifiers are there?
Pointers and
references

⋄ public
⋄ private
⋄ protected

10/23
Object Oriented Programming
Protected and Inheritance
Protected access specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Protected access specifiers can only be accessed within the class and by
Access Specifiers
Protected and
derived classes
Inheritance
Function overloading
Virtual function and
operator
polymorphism
Abstraction and
Namespace

Pointers and
references

12/23
Protected access specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Protected access specifiers can only be accessed within the class and by
Access Specifiers
Protected and
derived classes
Inheritance
Function overloading
Virtual function and
operator
polymorphism What is inheritance and derived class?
Abstraction and
Namespace

Pointers and
references

12/23
Protected access specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Protected access specifiers can only be accessed within the class and by
Access Specifiers
Protected and
derived classes
Inheritance
Function overloading
Virtual function and
operator
polymorphism What is inheritance and derived class?
Abstraction and
Namespace Inheritance is creating a new class, deriving from an existing class.
Pointers and
references

12/23
Protected access specifiers
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Protected access specifiers can only be accessed within the class and by
Access Specifiers
Protected and
derived classes
Inheritance
Function overloading
Virtual function and
operator
polymorphism What is inheritance and derived class?
Abstraction and
Namespace Inheritance is creating a new class, deriving from an existing class.
Pointers and
references
There are 3 kinds of access specification in inheritance,

⋄ private - derived members are private in base class


⋄ protected - derived members are protected in base class
⋄ public - derived members retain their access specifiers

12/23
Object Oriented Programming
Function overloading
Function overloading
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator Multiple functions can have same name but different parameters and
polymorphism
Abstraction and
Namespace
return type
Pointers and
references

14/23
Function overloading
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator Multiple functions can have same name but different parameters and
polymorphism
Abstraction and
Namespace
return type
Pointers and Since constructors are also functions you can have multiple constructors
references

14/23
Function overloading
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator Multiple functions can have same name but different parameters and
polymorphism
Abstraction and
Namespace
return type
Pointers and Since constructors are also functions you can have multiple constructors
references

Can you overload a destructor?

14/23
Function overloading
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator Multiple functions can have same name but different parameters and
polymorphism
Abstraction and
Namespace
return type
Pointers and Since constructors are also functions you can have multiple constructors
references

Can you overload a destructor?


NO

14/23
Object Oriented Programming
Virtual function and operator polymorphism
Virtual function and operator polymorphism
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator What to do when same function needs to do different operations in
polymorphism
Abstraction and
Namespace
different base class?
Pointers and
references

16/23
Virtual function and operator polymorphism
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator What to do when same function needs to do different operations in
polymorphism
Abstraction and
Namespace
different base class?
Pointers and Use virtual functions,
references

16/23
Virtual function and operator polymorphism
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator What to do when same function needs to do different operations in
polymorphism
Abstraction and
Namespace
different base class?
Pointers and Use virtual functions, it will help in overriding functions from super class
references

16/23
Virtual function and operator polymorphism
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator What to do when same function needs to do different operations in
polymorphism
Abstraction and
Namespace
different base class?
Pointers and Use virtual functions, it will help in overriding functions from super class
references

How to make an operator work in a different way?

16/23
Virtual function and operator polymorphism
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
Function overloading
Virtual function and
operator What to do when same function needs to do different operations in
polymorphism
Abstraction and
Namespace
different base class?
Pointers and Use virtual functions, it will help in overriding functions from super class
references

How to make an operator work in a different way?


Using operator overloading

16/23
Object Oriented Programming
Abstraction and Namespace
Header file and Namespace
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
⋄ Class definition and implementation can be written in a separate file
Function overloading
Virtual function and
operator
⋄ Header files have .h extension
polymorphism
Abstraction and
Namespace
⋄ Implementation can be in a different file
Pointers and
references

18/23
Header file and Namespace
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
⋄ Class definition and implementation can be written in a separate file
Function overloading
Virtual function and
operator
⋄ Header files have .h extension
polymorphism
Abstraction and
Namespace
⋄ Implementation can be in a different file
Pointers and
references
As the code grows how do you organize your code?

18/23
Header file and Namespace
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
⋄ Class definition and implementation can be written in a separate file
Function overloading
Virtual function and
operator
⋄ Header files have .h extension
polymorphism
Abstraction and
Namespace
⋄ Implementation can be in a different file
Pointers and
references
As the code grows how do you organize your code?

⋄ You use namespaces

18/23
Header file and Namespace
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
⋄ Class definition and implementation can be written in a separate file
Function overloading
Virtual function and
operator
⋄ Header files have .h extension
polymorphism
Abstraction and
Namespace
⋄ Implementation can be in a different file
Pointers and
references
As the code grows how do you organize your code?

⋄ You use namespaces


⋄ Namespace helps in avoiding name conflicts

18/23
Header file and Namespace
Object Oriented
Programming
Classes and Objects
Example of Classes
and Objects
Constructors and
Destructors
Access Specifiers
Protected and
Inheritance
⋄ Class definition and implementation can be written in a separate file
Function overloading
Virtual function and
operator
⋄ Header files have .h extension
polymorphism
Abstraction and
Namespace
⋄ Implementation can be in a different file
Pointers and
references
As the code grows how do you organize your code?

⋄ You use namespaces


⋄ Namespace helps in avoiding name conflicts
⋄ Namespace can also be nested

18/23
Pointers and references
Pointers and references
Pointers
Object Oriented
Pointers
Programming

Pointers and
references
Pointers
Smart pointers and
dynamic memory
allocation

⋄ Pointer stores address, * operator is used to declare a pointer


⋄ *p gives value, &x gives address.
⋄ Used mainly for dynamic memory allocation.

21/23
Pointers and references
Smart pointers and dynamic memory allocation
Smart pointer and dynamic memory allocation
Object Oriented
Programming

Pointers and
references
Pointers
Smart pointers and
dynamic memory
allocation

⋄ Lets be real, handling memory and pointers are hard!!

23/23
Smart pointer and dynamic memory allocation
Object Oriented
Programming

Pointers and
references
Pointers
Smart pointers and
dynamic memory
allocation

⋄ Lets be real, handling memory and pointers are hard!!


⋄ To solve this we use special pointers

23/23
Smart pointer and dynamic memory allocation
Object Oriented
Programming

Pointers and
references
Pointers
Smart pointers and
dynamic memory
allocation

⋄ Lets be real, handling memory and pointers are hard!!


⋄ To solve this we use special pointers
⋄ There are three kind; unique, shared and weak pointers

23/23
Smart pointer and dynamic memory allocation
Object Oriented
Programming

Pointers and
references
Pointers
Smart pointers and
dynamic memory
allocation

⋄ Lets be real, handling memory and pointers are hard!!


⋄ To solve this we use special pointers
⋄ There are three kind; unique, shared and weak pointers
⋄ Dynamic memory allocation is done using new and delete.

23/23
Smart pointer and dynamic memory allocation
Object Oriented
Programming

Pointers and
references
Pointers
Smart pointers and
dynamic memory
allocation

⋄ Lets be real, handling memory and pointers are hard!!


⋄ To solve this we use special pointers
⋄ There are three kind; unique, shared and weak pointers
⋄ Dynamic memory allocation is done using new and delete.
⋄ Book keeping of pointers in huge codebase helps in debugging
quickly!!!

23/23
Thanks for your attention!

You might also like