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

2.lab 2 - Classes Part 2

This document outlines the key concepts covered in an experiment on implementing classes and objects in C++, including: defining classes with data members and member functions; declaring and initializing objects; constructors for initializing data members; constructor overloading; destructors; passing objects as function arguments; and defining friend functions. The experiment provides examples of how each concept works in C++ code.

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
19 views

2.lab 2 - Classes Part 2

This document outlines the key concepts covered in an experiment on implementing classes and objects in C++, including: defining classes with data members and member functions; declaring and initializing objects; constructors for initializing data members; constructor overloading; destructors; passing objects as function arguments; and defining friend functions. The experiment provides examples of how each concept works in C++ code.

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
You are on page 1/ 13

EE-203 DS&A Lab

Experiment 2:
“Implement Classes & Objects using C++
Part II”

Batch 8 Fall 2020


Previous Lab Outline
 What is a class in C++

 Defining a class using C++

 Members of a class
 Data Members

 Member functions

 Member Access Specifiers

 What is an object in C++

 Declaring Objects of a class

 Calling member functions through classes

 Defining member functions outside the class

Current Lab Outline


 Storage of Objects in Memory

 Constructors

 Initializing Data using Constructors

 Constructor Overloading

 Destructors

 Passing Objects as Arguments

 Friend Function
Storage of Objects in Memory
 Data members are stored separately for each object
 Member functions are stored once, used by all objects
 Each object of a class has separate memory for their data
members, However, the member functions of a class are stored
at one place and shared by all objects of that class
Example:
Constructors
 A constructor is a member function of a class that is called and
executed automatically when an object of that class is created.
 Name of constructor function is same as name of class itself.
 A constructor function have many arguments but it can not
return any value.

• Member function ‘test()’ is the


constructor function
• Each time an object of class ‘test’
is created, the constructor is
executed and ‘Welcome’ is
printed.
Initializing Data Using Constructors
 The constructors functions are used to initialize data members,
when objects of a class are created.
 This type of initialization is called automatic initialization

The constructor function


‘sum()’ assigns values to the
data members of objects ‘a’
& ‘b’ and calculates their
sum which is then printed on
call to ‘psum’
Constructor Overloading
 Defining more than one constructor with a different set of
argument parameters is known as constructor overloading
 Constructor overloading is used to initialize different values to the
class objects
 When an object of a class using constructor overloading is
created, the corresponding constructor that matches the number
of parameters of the object function is executed.

• When object ‘x’ is created


the sum() having 2
arguments is called.
• When object ‘y’ is created
the sum() having 3
arguments is called.
Destructors
 When an object of a class is destroyed, a special function called
destructor function is executed automatically.
 Destructor has the same name as the class, but a tilde sign (~) is
written before its name.
 Destructor is executed automatically when an object comes to
the end of its life.
 They have no return value or arguments
 Local object destroyed where the function it is defined in, ends.
 Global objects are destroyed at the end of main function
 Destructors are used to free the memory that was allocated for
objects
Example
Passing objects as arguments
 Objects can be passed as arguments to member functions
 When passing to a member function:
 Only name of object is written in argument
Friend Function
 A non-member function of a class able to access all members of
that class is known as friend function.
 Able to access private members of class
 It is defined in a class using keyword friend before the name of
the function in declarator. Can be defined anywhere in the class
 For defining outside class, scope resolution operator :: is not used.
Example

You might also like