0% found this document useful (0 votes)
6 views

EEE100 Lecture 8 - Classes and Object

The document outlines the fundamentals of Object-Oriented Programming (OOP) focusing on classes and objects, including their definitions, constructors, destructors, and access modifiers. It emphasizes the importance of encapsulation, memory management through shallow and deep copying, and advanced features like the this pointer and friend functions. Overall, it serves as a guide for implementing OOP principles to solve real-world problems in programming.

Uploaded by

remo.leba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

EEE100 Lecture 8 - Classes and Object

The document outlines the fundamentals of Object-Oriented Programming (OOP) focusing on classes and objects, including their definitions, constructors, destructors, and access modifiers. It emphasizes the importance of encapsulation, memory management through shallow and deep copying, and advanced features like the this pointer and friend functions. Overall, it serves as a guide for implementing OOP principles to solve real-world problems in programming.

Uploaded by

remo.leba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CLASSES AND OBJECT

EEE100 Basic Programming for Electrical


Engineering

Engr. Rogelio F. Bersano Jr.


Instructor
Course Objectives:
❖ Understand the fundamentals of OOP by defining classes, creating
objects, and using access modifiers (public/private) for
encapsulation.
❖ Implement constructors and destructors to manage object
initialization and cleanup, including overloaded and copy
constructors.
❖ Differentiate between shallow and deep copying and use move
constructors for efficient resource management.
❖ Utilize advanced class features like the this pointer, friend
functions, and static members to design robust programs.
❖ Apply OOP principles to solve real-world problems, including
designing singleton classes and avoiding common pitfalls (e.g.,
memory leaks).
3

The Concept of Classes and Objects


▪A class is a blueprint for creating objects
(instances).
▪ An object is an instance of a class, encapsulating
data (attributes) and behavior (methods).
4

Declaring a Class

Syntax:

Example:
5

Instantiating an Object
Objects are created from a class using the class name.

Example:
6

Accessing Members
• Dot operator (.): Used for objects.
• Arrow operator (->): Used for pointers to objects.

Example:
7

Accessing Members
• Dot operator (.): Used for objects.
• Arrow operator (->): Used for pointers to objects.

Example:
8

Keywords public and private


• public: Members accessible from outside the class.
• private:Members accessible only within the class
(encapsulation).

Example:
9

Constructors
Special member functions called when an object is created.

Default Constructor:

Constructor Overloading:
10

Constructors
Special member functions called when an object is created.
Default Constructor:

Parameterized Constructor:

Constructor Overloading:
11

Destructor
Called when an object is destroyed (e.g., goes out of scope).

Example:
12

Copy Constructor
A copy constructor initializes an object by copying another
object.
Example:
13

Copy Constructor
Shallow vs. Deep Copy:
▪ Shallow copying can lead to multiple objects pointing to the
same memory, causing issues on deletion.

Example:
14

Copy Constructor
Shallow vs. Deep Copy:
▪ A deep copy ensures each object has its own copy of
dynamically allocated memory.

Example:
15

Move Constructors Help Improve Performance


Move constructors transfer ownership of resources instead of
copying them.
Syntax:

Example:
16

this Pointer
The this pointer refers to the current object.

Example:
17

struct vs. class


• struct: Default members are public.
• class: Default members are private.
Example:
18

friend Keyword
Allows external functions/classes to access private members.

Example:
19

Summary
• Classes encapsulate data and behavior.
• Constructors initialize objects; destructors clean up.
• Copy/move constructors manage object copying efficiently.
• public/private enforce encapsulation.
• this refers to the current object.
• struct and class differ in default access.
END

You might also like