You are on page 1of 20

OBECT ORIENTED

PROGRAMMING
Week 06 - Constructors & Destructors
Class Ingredient
class Ingredient:
{
string name;
double quantity;
string unit;
}
Ingredient i1;
i1.quantity+=1; // error

Fall 2023 CS 212 2


Constructor
◼ A special member function: to initialize an
object of the class when the object is created.
❑ must have the same name as the class.
❑ Never return any value (not even void).
❑ Declared as public.
◼ Usually, the data members of the class
are initialized inside the constructor.
◼ The constructor is called when an object of
this class is created.

Fall 2023 CS 212 3


Class Player
class Player{
string name;
int matchesPlayed;
int runsScored;
int wicketsTaken;
public:
void print_data();
};

Fall 2023 CS 212 4


Class Player

class Player{
class attributes …
public:
Player(); # default constructor
void print_data();
};

Fall 2023 CS 212 5


Default Constructor

◼ The compiler provides a default constructor


for you – a constructor with no parameters.
◼ You may provide a default constructor explicitly.
Player() # default constructor
{
matchesPlayed = 0;
runsScored = 0;
wicketsTaken = 0;
}

Fall 2023 CS 212 6


Overloaded Constructors
provides flexibility in creating multiple types of objects for a class.

◼ Overloaded Functions: Functions having


the same name but different sets of
parameters.
❑ The parameter types
❑ The number of parameters
❑ The order of the parameter types
◼ Overloaded Constructors: Constructors with
different sets of parameters.

Fall 2023 CS 212 7


Class Player

class Player{
class attributes …
public:
Player();
Player(string str);
void print_data();
};

Fall 2023 CS 212 8


Class Ingredient
class Ingredient{
private:
string name, unit;
double quantity;
public:
Ingredient() : name(“”), quantity(0.0),
unit(“”) {}

Ingredient(string name) : name(name) {}

Ingredient(string n, double q, string u) :


name(n), quantity(q), unit(u) {} };
Fall 2023 CS 212 9
Constructor with Arguments vs. Default
Constructor
◼ If a constructor with parameters (arguments)
is defined in a class, C++ will not implicitly
create a default constructor for this class.
◼ If you want to have a default constructor,
explicitly define it by yourself.

Fall 2023 CS 212 10


Class Player

Player(){
matchesPlayed = 0;
runsScored = 0;
wicketsTaken = 0;
}

Player(string str){
Player();
name = str;
}

Fall 2023 CS 212 11


Class Player

Player(): matchesPlayed(0), runsScored(0),


wicketsTaken(0){} # preferable syntax

Player(string str){
Player(); # set all variables to 0
name = str; # overwrite name
}

Fall 2023 CS 212 12


Initializer List in Constructors – All
Possible Ways
Box() { Box() :
length = 0; length(0),width(1),height(1)
width = 0; {}
height = 0;
}

Box(double l, double w, double h){ Box(double l, double w, double h) :


length = l; length(l),width(w),height(h)
width = w; {}
height = h;
}

Fall 2023 CS 212 13


Copy Constructor
◼ The compiler generates a default version of what is
referred to as a copy constructor.
◼ A copy constructor creates an object of a class by
initializing it with an existing object of the same class.
◼ The default version of copy constructor creates new
object by copying existing object member by member.
int main(){
Player p1;

Player p2(p1);
}

Fall 2023 CS 212 14


Destructors
◼ A special member function
◼ Name is the tilde character (~) followed by the
class name, e.g., ~Player( )

◼ Called implicitly when an object is destroyed


❑ For example, this occurs as an automatic object is
destroyed when program execution leaves the
scope in which that object was instantiated

Fall 2023 CS 212 15


Destructors (Cont.)

◼ Receives no parameters and returns no value


❑ May not specify a return type—not even void.
◼ A class may have only one destructor
❑ Destructor overloading is not allowed
◼ If the programmer does not explicitly provide
a destructor, the compiler creates an “empty”
destructor.

Fall 2023 CS 212 16


Class Player

class Player{
class attributes …
public:
Player();
~Player(){} //destructor
Player(string str);
void print_data();
};

Fall 2023 CS 212 17


Getters and Setters

◼ Setters (mutator methods) in C++ are used to


modify the private members of a class,
ensuring controlled access and data integrity.

◼ Getters (accessor methods) are used to


retrieve the values of private members,
encapsulating the class's data and allowing
controlled read-only access.

Fall 2023 CS 212 18


Class Player
class Player{
private:
class attributes …
public:
void setAge(int _age){
if(_age>20){age=_age;}
}
void getAge(){return age;}
};
Fall 2023 CS 212 19
Class Player

Player p1;
cout << p1.age; # error
p1.age = 20; # error
p1.setAge(30);
cout << p1.getAge();

Fall 2023 CS 212 20

You might also like