Professional Documents
Culture Documents
WRITTEN BY:
ACSC 328: Object Oriented Copyright © UNIVERSITY OF EMBU, JANUARY 2022
Dr. E.C TOO 1
Programming(C++) All Rights Reserved
Constructors and Destructors in C++
Constructors are special class functions which performs initialization
of every object.
The Compiler calls the Constructor whenever an object is created.
Constructors initialize values to object members after storage is
allocated to the object.
Destructor on the other hand is used to destroy the class object.
C++ Constructors
• class Area
• {
• public:
• int x;
• // constructor
• Area()
• {
• // object initialization
• }
• };
• Constructors can be defined either inside the class definition or outside
class definition using class name and scope resolution :: operator.
• class Area
•{
• public:
• int i;
• Area(); // constructor declared
• };
• // constructor definition
• Area::Area()
• {
• i = 1;
• }
Types of Constructors in C++
Access specifiers define how the members (attributes and methods) of a class
can be accessed.
three access specifiers:
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however,
they can be accessed in inherited classes.
• In the following example, we demonstrate the differences between public and private
members:
• class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
• Error: y is private
• Note: It is possible to access private members of a
class using a public method inside the same class. See
the next chapter (Encapsulation) on how to do this.
• Tip: It is considered good practice to declare your class
attributes as private (as often as you can).
• Note: By default, all members of a class are private if you don't
specify an access specifier:
• class MyClass {
int x; // Private attribute
int y; // Private attribute
};
EXERCISES
• Write a program to print the volume of a box by creating a class
named 'Volume' with an initialization list to initialize its length,
breadth and height. (just to make you familiar with initialization lists)
• Write a program that would print the information (name, year of
joining, salary, address) of three employees by creating a class named
'Employee'. The output should be as follows:
• Name Year of joining Address
• Robert 1994 64C- WallsStreat
• Sam 2000 68D- WallsStreat
• John 1999 26B- WallsStreat
Software Engineering Observation 19.1
• As a rule of thumb, data members should be declared private
• Member functions should be declared public.
• Except member functions that are accessed only by other member functions
of the class.
ACSC 328: Object Oriented Introduction to Classes and Objects, Constructors and
25
Programming(C++) destructors
Summary
• Introduced class definitions and objects
• Public versus private access into class.
• Syntax for member functions
• Syntax data members
• Get and Set functions
• Constructors & Destructors
• Placing classes in separate files
• Separating interface from implementation