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

Constructors and Destructors: CSC-210: Object Oriented Programming

The document discusses constructors and destructors in C++ classes. It explains what they are, how they are defined, different types of constructors, and provides examples of using constructors and destructors in a Student class.

Uploaded by

Bilal Asad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Constructors and Destructors: CSC-210: Object Oriented Programming

The document discusses constructors and destructors in C++ classes. It explains what they are, how they are defined, different types of constructors, and provides examples of using constructors and destructors in a Student class.

Uploaded by

Bilal Asad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CSC-210: Object Oriented Programming

Constructors and Destructors


Lecture 03

By: Ms. Zupash Awais


Bahria University Lahore Campus
class Student
{
int id;
String name;
public:
void take(int i,String s)
{
Scanner obj= new Scanner(System.in);
id= obj.nextInt();

Java Code with obj.nextLine();


name = obj.nextLine();
Input }
void display()
{
System.out.print("ID: ");
System.out.print(id);
System.out.print("\nName: ");
System.out.println(name);
}
}
 A member function can be an accessor or a mutator
function

Member  Accessor Functions means Getter Functions (Use for


returning the information)
Functions  Mutator Functions means Getter Functions (Use for
setting/initializing the value of data members)
 Accessor and mutator functions (a.k.a. set and get
functions) provide a direct way to change or just access
private variables.

 They must be written with the utmost care because they


Accessors and must provide the protection of the data that gives
meaning to a class in the first place.
Mutators
 Remember the central theme of a class: data members
are hidden in the private section and can only be
changed by the public member functions which dictate
allowable changes.
class Student
{
private:
int id;
string name;
int age;
public:

Class Example void setid(int i);


void setname(string s);
void setage(int a);
int getid();
string getname();
int getage();
};
Student

- id : int
- name : string
- age : int

Class Design + setid(int) : void


+ setname(string) : void
+ setage(int) : void
+ getid() : int
+ getname() : string
+ getage() : int
class Student
{
private:
int id;
string name;
int age;
public:
Code Example void setid(int i) { id = i; }
void setname(string n) { name = n; }
void setage(int a) { age = a; }
int getid() { return id; }
string getname() { return name; }
int getage() { return age; }
};
int main()
{
Student s;
s.setid(10);
s.setname("M. Ali");
Code Example s.setage(20);

(…) cout << "ID: " << s.getid() << endl;


cout << "Name: " << s.getname() << endl;
cout << "Age: " << s.getage() << endl;
}
 A constructor is a special member function whose task
is to initialize the objects of its class.
 It is special because its name is same as the class
name.
Constructor  The constructor is invoked whenever an object of its
associated class is created.
 It is called constructor because it constructs the values
of data members of the class.
 There is no need to write any statement to invoke the
constructor function.
 If a ‘normal’ member function is defined for zero
initialization, we would need to invoke this function for
Constructor each of the objects separately.
 A constructor that accepts no parameters is called the
default constructor.
 The default constructor for class A is A : : A ( )
class Student
{
private:
int id;
string name;
int age;

C++ Class public:


Student()
Example {
id=0;
name=“ “;
age=0;
}
};
Student
- id : int
- name : string
- age : int
Class Design
+ Student()
 A destructor is used to destroy the objects that have been
created by a constructor.
 Like constructor, the destructor is a member function whose
name is the same as the class name but is preceded by a
tilde.
 A destructor never takes any argument, nor does it return
any value.
Destructor  It will be invoked implicitly by the compiler upon exit from
the program – or block or function as the case may be – to
clean up storage that is no longer accessible.
 It is a good practice to declare destructors in a program
since it releases memory space for further use.

 Whenever new is used to allocate memory in the


constructor, we should use delete to free that memory.
class Student
{
private:
int id;
string name;
int age;
public:

C++ Class Student()


{
Example cout<<“Object Created”;
}
~Student()
{
cout<<“\nObject Destroyed”;
}
};
Student
- id : int
- name : string
- age : int
Class Design
+ Student()
+ ~Student()
 They should be declared in the public section.
 They are invoked automatically when the objects are
created.

 They do not have return types, not even void and they
cannot return values.
Characteristics  Like other C++ functions, Constructors can have default
of Constructor arguments.
 We can not refer to their addresses.
 When a constructor is declared for a class initialization
of the class objects becomes mandatory
 Default Constructor
 Parameterized Constructor
Types of
 Typecast Constructor
Constructors  Copy Constructor
 It may be necessary to initialize the various data
elements of different objects with different values when
they are created.

Parameterized  This is achieved by passing arguments to the


Constructor constructor function when the objects are created.

 The constructors that can take arguments are called


parameterized constructors.
class Student

private:

int id;

string name;

int age;

public:

Student()

C++ Class {

cout<<“Object Created”;

Example }

Student(int i, string s, int a)

id=i;

name=s;

age=a;

};
Student
- id : int
- name : string
- age : int
Class Design
+ Student()
+ Student(int,string,int)
Write a program that defines a class Area, with data
members PI=3.1419 and radius r. and radius is input
by user. The program should use data members and
the functions listed to accomplish the following.

(a)value of a double variable r, which stores thePI is


Activity initialized through Constructor
(b)Prompt the user to input the radius of a sphere.

The program then outputs the following Surface area


of the sphere. 4*PI*R and area of circle PI*R2

You might also like