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

C++ - Day - 6 - Basic About OOP

Uploaded by

ductoan.hpftu
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)
23 views33 pages

C++ - Day - 6 - Basic About OOP

Uploaded by

ductoan.hpftu
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

C++ Training Course

Basic about OOP

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 1


Lesson Objectives

▪ Understand about characteristics of OOP


▪ Understand about Class
▪ Understand about Data encapsulation
▪ Understand about Access modifiers
▪ Understand about Constructors, Copy constructor,
Destructors

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 2


Section 1
Characteristics of OOP

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 3


Characteristics of OOP
▪ Encapsulation is capturing data and keeping it safely and securely from
outside interfaces
▪ Inheritance: This is the process by which a class can be derived from a
base class with all features of base class and some of its own. This
increases code reusability
▪ Polymorphism: This is the ability to exist in various forms. For example
an operator can be overloaded so as to add two integer numbers and
two floats.
▪ Abstraction- The ability to represent data at a very conceptual level
without any details.

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 4


Section 2
Class

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 5


Agenda

▪ What is Class
▪ What is object
▪ How to declare a class
▪ How to implement class

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 6


What is Class

▪ Class is an expanded concept of data structures:


like data structures, they can contain data members (also
called properties/attributes), but they can also contain
functions as members (also called methods/activities

class = data + functions

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 7


What is Class

class Dog {
▪ Properties:
public:
• Name
void eat();
• Age void sleep();
• Color void run();
• Weight void bark();

▪ Activities: private:
string mName;
• Eat
int mAge;
• Sleep string mColor;
• Run int mWeight;

• Bark };

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 8


What is Object

▪ An Object is an instantiation of a class ==> That means a


class is the a data type, and an Object is a variable of this
type

Dog dog; // dog is an object

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 9


Declare class

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 10


Implement class

void Rectangle::setValues(int x, int y) {


mWidth = x;
mHeight = y;
}

int Rectangle::getArea() {
return mWidth*mHeight;
}

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 11


Implement class

Rectangle.h Rectangle.cpp main.cpp


class Rectangle { #include "Rectangle.h" #include "Rectangle.h"
int mWidth; #include <iostream>
void Rectangle::setValues(int x, int y) using namespace std;
int mHeight; {
mWidth = x;
public: mHeight = y; int main() {
Rectangle rect;
void setValues(int, int); }
rect.setValue(3, 4);
int getArea(void); int getArea() { cout << "area = " << rect.getArea();
return mWidth*mHeight; return 0;
}; } }

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 12


Section 3
Data encapsulation

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 13


Agenda

▪ What is Encapsulation?
▪ Benefit?

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 14


What is Encapsulation

▪ Encapsulation (also called information hiding) is the


process of keeping the details about how an object is
implemented hidden away from users of the object.
▪ Users of the object access the object through a public
interface. In this way, users are able to use the object
without having to understand how it is implemented.
▪ All member variables of the class are made private (hiding
the implementation details), and most member functions
are made public (exposing an interface for the user)

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 15


What is Encapsulation

▪ All member variables of the class are made private (hiding the
implementation details), and most member functions are made
public (exposing an interface for the user)
class Rectangle {

private:
int mWidth;
int mHeight;

public:
void setValues(int, int);
int getArea(void);

};

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 16


Benefit

▪ Encapsulated classes are easier to use and reduce the


complexity of programs
▪ Encapsulated classes help protect your data and prevent
misuse
▪ Encapsulated classes are easier to change
▪ Encapsulated classes are easier to debug

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 17


Section 4
Access modifiers

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 18


Agenda

▪ Public Access Modifier


▪ Private Access Modifier
▪ Protected Access Modifier

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 19


Public Access Modifier
▪ The public keyword is used to create public members (data and
functions).
▪ The public members are accessible from any part of the program.

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 20


Private Access Modifier
▪ The private keyword is used to create private members (data and
functions).
▪ The private members can be accessed only from within the class.
▪ However, friend classes and friend functions can access private
members

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 21


Protected Access Modifier
▪ The protected keyword is used to create protected members (data and
function).
▪ The protected members can be accessed within the class and from the
derived class.

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 22


Section 4
Constructors, Copy constructor, Destructors

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 23


Agenda

▪ Constructors
▪ Copy constructor
▪ Destructors

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 24


Constructors
▪ A constructor is a special kind of class member function that is
automatically called when an object of that class is instantiated.
Constructors are typically used to initialize member variables of the
class to appropriate default or user-provided values, or to do any setup
steps necessary for the class to be used (e.g. open a file or database).
▪ Unlike normal member functions, constructors have specific rules for
how they must be named
✓ Constructors must have the same name as the class (with the same capitalization)
✓ Constructors have no return type (not even void)

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 25


Default constructors

▪ A constructor that takes no parameters is called a default


constructor

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 26


Parameterized Constructors
▪ To create a parameterized constructor, simply add parameters to it the
way you would to any other function. When you define the constructor’s
body, use the parameters to initialize the object

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 27


Copy constructor
▪ A copy constructor is a special type of constructor used to create a new object
as a copy of an existing object
▪ if we do not provide a copy constructor for your classes, C++ will create a public
copy constructor.

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 28


Destructors
▪ A destructor is another special kind of class member function that is
executed when an object of that class is destroyed
▪ Like constructors, destructors have specific naming rules:
✓ The destructor must have the same name as the class, preceded by a tilde (~).
✓ The destructor can not take arguments.
✓ The destructor has no return type. class Rectangle {
private:
int mWidth;
int mHeight;
public:
Rectangle(); // This is the default constructor
~Rectangle(); // This is the destructor
void setValues(int, int);
int getArea(void);
};
Rectangle::~Rectangle() {
cout << "Object is being deleted" << endl;
}

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 29


Member initializer
▪ What is different?

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 30


References

▪ https://www.tutorialspoint.com
▪ https://www.learncpp.com/

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 31


Lesson Summary

▪ 4 characteristics of OOP
▪ What is Class and Object
▪ Data encapsulation
▪ Public, Private, Protected
▪ Constructors, Copy constructor, Destructors

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 32


Thank you

3/29/2021 09e-BM/DT/FSOFT - ©FPT SOFTWARE – FSOFT Academy - Internal Use 33

You might also like