Chap6 OOP CCE
Chap6 OOP CCE
Programming
USJ - ESIB
Department of Electrical and Mechanical Engineering
Program: Computer and Communications Engineering
1
Object-Oriented Programming Chapter 6: OOP Basics
Course Outline
1. Basics of the C++ language
2. Changing the program flow with Control Statements
3. All about Functions
4. Arrays
5. Pointers, References, Structures and Memory Management
6. Object Oriented Programming (OOP) Basics
7. Operators Overloading in OOP
8. Inheritance and Polymorphism in OOP
9. More on OOP (Static members, Namespaces, Exceptions, Templates, File
Management)
The program is divided into small parts The program is divided into small parts called objects.
called functions.
Less secure. Doesn’t provide a proper way for data More secure. Data protection is possible by hiding
hiding. sensitive data and functions.
Less robust. Data integrity is not guaranted. More robust by ensuring data integrity and
consistency.
Modification and code expansion is more difficult and Modification and code expansion is easy and fast.
complex.
Not suitable for complex applications. Can be used for Suitable for complex applications and software
simple programs. development.
Interface Accessible
What is visible methods
Attributes to Methods to
Implementation be hidden be hidden
What is hidden
Interface computeArea()
What is visible computeVolume()
display()
name
height
Implementation width
What is hidden length
• Abstraction: means hiding implementation code that is not necessary for use by
other objects. This helps make it easier for developers to change the object
implementation over time without changing the behavior of the object when it’s
used. kel objet aw class bi alba operations that works on these object yaane kel shi khaso bel objet la ymshe wel objet mawjud bi mahal wahad, je peux securisee mes
donne w ana baarir la min baate acces
Advantages of OPP
Modularity
Robustness Readability
Advantages
of OOP
Data
Reusabilty
protection
Maintainability
Access modifiers
Class constructors kl m ekhla2 objet bhtobel construction fye aati argument lal constructeur w fye par
defaut ya explcite mtl qd je cree un objet avec valeur w class
ya implicite huwe waet ekhla2 objet bala class huwe b c++ b zabto w b hoto bi
memory m lezim ne3temedaa
• The constructor has same name as the class itself. class bekhla2la constructeur w attributes
Default constructor
class Room {
public:
//method prototypes
Room(double len=2, double width=2, double height=2);
It’s possible to initialize attributes when
string getName() const;
they are declared (double height = 15).
void setName(string);
However, this is not recommended. It’s
double computeArea() const;
better to initialize them through the
double computeVolume() const;
constructor.
void display() const;
private:
//private attributes
string name;
double height;
double width;
double length;
}
Parametrized constructor
• It’s a constructor having at least one mandatory argument. The
presence of a parametrized constructor cancels the role of the default
constructor.
class Room { Room room1 (7, 8.8, 4);
public: Room room2 {12.5};
//parametrized constructor prototype Room room3;
Room(double length, double width=2, double height=2);
.
.
.
//Constructor definition
Room::Room(double length, double width, double height)
: length(length), width(width), height(height) {}
Parametrized constructor
• It’s a constructor having at least one mandatory argument. The
presence of a parametrized constructor cancels the role of the default
constructor unless we add Room()=default; inside the class.
class Room { Room room1 (7, 8.8, 4);
public: Room room2 {12.5};
//parametrized constructor prototype Room room3;
Room(double length, double width=2, double height=2);
Room() = default;
.
.
.
//Constructor definition
Room::Room(double length, double width, double height)
: length(length), width(width), height(height) {}
Destructor
Destructor
#include <iostream>
#include <memory>
using namespace std;
class MyClass{
public:
MyClass(){cout << "Object created" << endl;;}
~MyClass(){cout << "Object destroyed" << endl;}
int getA(){return a;}
void setA(int b){a=b;} Start of block
private: Object created
int a;
Object destroyed
};
int main() End of block
{
cout << "Start of block" << endl;
{
MyClass myclass;
myclass.setA(9);
}
cout << "End of block" << endl;
}
Copy Constructor
• A copy constructor is a member function that initializes an object using another object of
the same class. It creates an object by initializing it with an object of the same class
which has been created previously.
• The copy constructor can be defined explicitly by the programmer. If the programmer
does not define the copy constructor, the compiler does it for us.
• The copy constructor is called when:
• When an object of the class is returned by value.
• When an object of the class is passed (to a function) by value as an argument.
• When an object is constructed based on another object of the same class.
• MyClass obj1(arg1, arg2, …);
• MyClass obj2(obj1);
• MyClass obj3 = obj1;
Smart Pointers
• Consider this example:
void my_func()
{
int* valuePtr = new int(15);
int x = 45;
if (x == 45){
delete valuePtr;
}
If x == 45, the function returns without freeing the allocated memory. One possible solution is to use Smart Pointers.
USJ-ESIB Electrical and mechanical department – CCE Program 30
Object-Oriented Programming Chapter 6: OOP Basics
Smart Pointers
#include <memory>
using namespace std;
void my_func()
{
unique_ptr<int> valuePtr(new int(15));
int x = 45;
// ...
if (x == 45)
return; // no memory leak anymore!
// ...
}
{
unique_ptr<int> p = make_unique<int>();
// make use of p