0% found this document useful (0 votes)
6 views32 pages

Lecture7 8 ManagerFunctions StaticType

Uploaded by

yadim96622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views32 pages

Lecture7 8 ManagerFunctions StaticType

Uploaded by

yadim96622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 7

Constructors & Destructors

Class in C++ 43
Class in C++ 44
Implicit and explicit inline function

Class in C++ 45
Lifecycle of Object

Class in C++ 46
Constructors
• Member function that is automatically called
when an object is created
• Purpose is to construct an object and do
initialization if necessary
• Constructor function name is class name
• Has no return type specified

Class in C++ 47
Types of constructor:Default
Constructors
• A default constructor is a constructor that takes no
arguments.
• If you write a class with no constructor at all, C++ will
write a default constructor for you, one that does
nothing.(Implicitly-(automatically) created and called
by the compiler)
• A simple instantiation of a class (with no arguments)
calls the default constructor:
Rectangle r;

Class in C++ 48
Types of Constructors: Parameterized
Constructor
• Parameterized Constructor:
• It is possible to pass arguments to
constructors.
• These arguments help initialize an object
when it is created.
• To create a parameterized constructor,
simply add parameters to it (the
same way as to any other function).
• When defining the constructor’s body, use
the parameters to initialize
the object.
• Uses:
• To initialize various data elements of
different objects with diffferent values
when they are created
• Used to overload constructors
• Can be called implicitly or explicitly
• Point p1 = Point(10,20) // Explicit call
• Point p1(10,20) // Implicit call
Parameterized Constructor:
Passing Arguments to Constructors
• To create a constructor that takes arguments:
– indicate parameters in prototype:
Rectangle(double, double);

– Use parameters in the definition:


Rectangle::Rectangle(double w, double
len)
{
width = w;
length = len;
}

Class in C++ 50
Passing Arguments to Constructors
• You can pass arguments to the constructor when you
create an object:

Rectangle r(10, 5);// Implicit


Or Rectangle r= Rectangle(10,5)//explicit

Class in C++ 51
Overloading Constructors
• A class can have more than one constructor
Overloaded constructors in a class must have
different parameter lists:
Rectangle();
Rectangle(double);
Rectangle(double, double);

Class in C++ 52
Rectangle rd;
Rectangle rp=Rectangle(25);
Rectangle rc(10,50);

Class in C++ 53
More About Default Constructors
• If all of a constructor's parameters have default
arguments, then it is a default constructor. For
example:

Rectangle(double = 0, double = 0);

• Creating an object and passing no arguments


will cause this constructor to execute:

Rectangle r;

Class in C++ 54
Classes with No Default Constructor
• When all of a class's constructors require
arguments, then the class has NO default
constructor
• When this is the case, you must pass the
required arguments to the constructor when
creating an object

Class in C++ 55
Destructors
• Member function automatically called when
an object is destroyed
• Destructor name is ~classname, e.g.,
~Rectangle
• Has no return type; takes no arguments
• Only one destructor per class, i.e., it cannot be
overloaded
• If constructor allocates dynamic memory,
destructor should release it

Class in C++ 56
Only One Default Constructor
and One Destructor
• Do not provide more than one default constructor
for a class: one that takes no arguments
or one that has default arguments for all parameters
Square();
or
Square(int = 0);

• Since a destructor takes no arguments, there can


only be one destructor for a class

Class in C++ 57
Lecture 8

Copy Constructors &


Static variables

Class in C++ 58
Types of Constructors: Copy Constructor
• A Member function which initializes an object using
another object of the same class
• Prototype:
• className (const className &oldObject);
• After calling the copy constructor, the source and the
destination objects have the same value for each data
member, although they are different objects.

• The copy constructor has only one parameter that


receives the source object by reference.
• The const modifier in front of the parameter type
guarantees that the pass-by-reference cannot change the
source object.
• Called when:
• When an object of the class is passed (to a function)
by value as an argument.
• When an object is constructed based on another object
of the same class
Copy Constructor
•Copy Constructor is a type of constructor which is used to create a copy
of an already existing object of a class type.
•The copy constructor is a constructor which creates an object by initializing it with an object
of the same class, which has been created previously.
•The copy constructor is used to:

[Link] one object from another of the same type.


[Link] an object to pass it as an argument to a function.
[Link] an object to return it from a function.

•If a copy constructor is not defined in a class, the compiler itself defines one.
•the class has pointer variables and has some dynamic memory allocations,
• then it is a must to have a copy constructor.
•it is usually of the form X (X&), where X is the class name.
•The compiler provides a default Copy Constructor to all the classes.

Syntax:
classname (const classname &obj) { // body of constructor }
Here, obj is a reference to an object that is being used to initialize another object

Class in C++ 60
class Point
{
private:
int x, y;
public:
Point(){x=y=0;}
Point(int x1, int y1) { x = x1; y = y1; }
Point(const Point &p1) {x = p1.x; y = p1.y; }// Copy constructor

int getX()const { return x; }


int getY()const { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
//Point p2 = p1; // Copy constructor is called here
//or
Point p2(p1);// Copy constructor
// Let us access values assigned by constructors
cout << "p1.x = " << [Link]() << ", p1.y = " << [Link]();
cout << "\np2.x = " << [Link]() << ", p2.y = " << [Link]();
return 0;
} Class in C++ 61
Member Function Overloading
• Non-constructor member functions can
also be overloaded:
void setCost(double);
void setCost(char );

• Must have unique parameter lists as for


constructors

Class in C++ 62
Arrays of Objects
• Objects can be the elements of an array:

Rectangle rooms[8];

• Default constructor for object is used when


array is defined

Class in C++ 63
Arrays of Objects
Must use initializer list to invoke constructor that
takes arguments:

Rectangle rArray[3]={Rectangle(2.1,3.2),
Rectangle(4.1, 9.9),
Rectangle(11.2, 31.4)};

Class in C++ 64
Arrays of Objects
• It isn't necessary to call the same constructor
for each object in an array:
Rectangle
rArray[3]={Rectangle(2.1,3.2),
Rectangle(),
Rectangle(11.2, 31.4)};

Class in C++ 65
Accessing Objects in an Array
• Objects in an array are referenced using subscripts

• Member functions are referenced using dot notation:

rArray[1].setWidth(11.3);
cout << rArray[1].getArea();

Class in C++ 66
Static Data member
• Data member of a class can be specified as static
• The properties of a static member variable are similar to that of a
static variable in C.
• It is initialized to zero when the first object of its class is created. No
other initialization is permitted.
• Only one copy of that member is created for the entire class and is
shared by all the objects of that class, no matter how many objects
are created.
• It is visible only with in the class but its life time is the entire
program.
• Static variables are normally used to maintain values common to
the entire class.
• For example a static data member can be used as a counter that
records the occurrence of all the objects.
• Type and scope of each static member variable
must be defined outside the class definition .
• This is necessary because the static data
members are stored separately rather than as a
part of an object.
• int item :: count; // definition of static data
member
• We can access static member inside the member
functions or inside constructor
Class in C++ 69
class item
{
static int count; //count is static
public:
item()
{
count++;
cout<<count<<endl;
}
};
int item :: count; //static variable explicitly initialized
int main( )
{ item a,b,c; }

6-Oct-21 70
class item
{
public:
static int count; //count is static
item(){count++;}
};
int item :: count=2;
int main( )
{
cout<<item::count;
item a,b,c;
cout<<item::count;
[Link]++;
cout<<[Link];
}

6-Oct-21 71
static member function
• By declaring a function member as static, you make it
independent of any particular object of the class.
• A static member function can be called even if no
objects of the class exist and the static functions are
accessed using only the class name and the scope
resolution operator ::
• A static member function can have access to only other
static members declared in the same class.
• A static member function can be called using the class
name as follows:-
classname :: functionname();
#include <iostream>
int Example:: Number;
using namespace std;

class Example{ int main()


static int Number; {
int n; Example::show_Number();

public: Example example1, example2;


void set_n()
{ example1.set_n();
n = ++Number; example2.set_n();
}
example1.show_n();
void show_n() example2.show_n();
{
cout<<"value of n = "<<n<<endl;
}
Example::show_Number();
static void show_Number(){
return 0;
cout<<"value of Number = "<<Number<<endl; }
}
};
Class in C++ 73
Class in C++ 74

You might also like