Note 02
Note 02
CS10003
Lecturer: Do Nguyen Kha
Contents
● Object-Oriented Programming (OOP)
● Class and Object
● Access Modifiers
● Method
● Encapsulation
● The this pointer
● Dynamic Memory Allocation
● UML
Object-Oriented Programming (OOP)
“Object-oriented programming (OOP) is a programming paradigm based on the
concept of objects, which can contain data and code: data in the form of fields
(often known as attributes or properties), and code in the form of procedures
(often known as methods).” - Wikipedia
Class and Object
A class is a definition of objects of the same kind. In other words, a class is a
blueprint, template, or prototype that defines and describes the attributes and
behaviors common to all objects of the same kind.
public:
int serial;
string manufacturer;
string color;
bool isEV;
};
Create an object
Car car1;
Car car2, aSpecialCar;
car1.serial = 123456;
car1.manufacturer = "Tesla";
car1.isEV = true;
car1.color = "black";
cout << car1.color;
cin >> car1.color;
Access Modifiers
In C++, there are three access specifiers:
● Data Hiding
● Access control
● Abstraction
● Maintainability and Flexibility
Encapsulation
class Employee {
private:
int salary;
public:
// Setter
void setSalary(int s) {
this.salary = s;
}
// Getter
int getSalary() {
return this.salary;
}
};
The this pointer
In C++, this is a keyword that refers to the current instance of the class.
Dynamic Memory Allocation
Employee* e1 = new Employee();
e1->setSalary(10000);
delete e1;
UML
The UML (Unified Modeling Language) Class diagram is a graphical notation used
to construct and visualize object oriented systems:
● Classes
● Attributes
● Methods
● and the relationships among objects
https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/
Coding Convention
● Each class must be defined in a header file (.h): Employee.h
○ Using #define guard to prevent multiple inclusions
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
…
#endif // EMPLOYEE_H_
● Class method is implemented in a C++ source code file (.cpp): Employee.cpp