Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lecture-9 (Constructors)

The document provides an overview of classes and constructors in C++. It explains the purpose and characteristics of constructors, including default constructors, and illustrates their usage through examples. Additionally, it highlights the importance of constructors in initializing class objects and managing member variables.

Uploaded by

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

Lecture-9 (Constructors)

The document provides an overview of classes and constructors in C++. It explains the purpose and characteristics of constructors, including default constructors, and illustrates their usage through examples. Additionally, it highlights the importance of constructors in initializing class objects and managing member variables.

Uploaded by

farhanabid327426
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Recape:Class

class classname
{
private:
member variables
public:
member functions
};

1
Recap: Class Example
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class smallobj { //define a class
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << "Data is " << somedata << endl;
}
}; 2
Recap: Class Example
int main() Objects of Class
{
smallobj s1, s2;
Setting Values
via objects
s1.setdata(1066);
s2.setdata(1776);
Showing Values
via objects
s1.showdata();
s2.showdata();
return 0;
}
3
What is a constructor?
Constructor is used to initialize the objects of
a class
Constructor is used to ensure that object is in
well defined state at the time of creation
Constructor is automatically called when the
object is created
Constructor are not usually called explicitly
What is a constructor?
Constructor is a special function having same
name as the class name
Constructor does not have return type
Constructors are commonly public members
Class
Class Student
Student
{ Class

Private:
int id;
Public:
Student
{ Default
Constructor
}
};

6
Comments on constructors
 Constructor is a function in every class which is called
automatically when class creates its object
 Basically it helps in initializing data members of
the class
 A class may have multiple constructors
 If you do not specify a constructor, the compiler
generates a default constructor for you (expects no
parameters and has an empty body).
Default Constructor
Constructor without any argument is called
default constructor
If we do not define a default constructor the
compiler will generate a default constructor
This compiler generated default constructor
initialize the data members to their default
values
Example
class Student
{
int rollNo;
char *name;
float GPA;
public:
… //no constructors
};
Example
Compiler generated default constructor
{
rollNo = 0;
GPA = 0.0;
name = NULL;
}
Default Constructor

 The default constructor is the constructor that


takes no parameters
It is special because it is called when an object
is declared
But not initialized with any arguments.
Default Constructor
Rectangle rectb; // ok, default constructor called
Rectangle rectc(); // oops, default constructor NOT called
The default constructor is called for rectb.
rectb is not even constructed with an empty set of
parentheses
In fact, empty parentheses cannot be used to call
the default constructor:
This is because the empty set of parentheses would
make of rectc a function declaration instead of an
object declaration
It would be a function that takes no arguments and
returns a value of type Rectangle.
Example 2
#include <iostream>
using namespace std;
class Counter
{
private:
int count ;
public:
Counter(): count(0) //constructor
{
/*empty body*/
}
void inc_count() //increment count
{
count++;
}
Example 2…
int get_count() //return count
{
return count;
}
};
int main()
{
Counter c1, c2; //define and initialize

cout << "\nc1=" << c1.get_count(); //display


cout << "\nc2=" << c2.get_count();

c1.inc_count(); //increment c1
c2.inc_count(); //increment c2
c2.inc_count(); //increment c2

cout << "\nc1=" << c1.get_count(); //display again


cout << "\nc2=" << c2.get_count();
return 0;
}
Initialization by Constructor
Counter(): count(0) //constructor
{
/*empty body*/
}

or

Counter() //constructor
{
count = 0;
}
Multiple members initialize
If multiple members must be initialized,
they’re separated by commas.
The result is the initializer list (sometimes
called by other names, such as the member-
initialization list).
counter() : m1(7), m2(33), m2(4)
{
/*empty body*/
}
What is the output?
#include<iostream.h> void showData()
class Distance {
{ cout<<feet<<" feet and
private: "<<inches<<"inches\n";
int feet; }
float inches; };
public:
Distance () void main()
{ {
feet=0; Distance d1;
inches=0.0; d1.showData();
}
d1.getData();
void getData() d1.showData();
{
cout<<"Enter Feet\n";
cin>>feet; }
cout<<"Enter Inches\n";
cin>>inches;
}
Same Name as the Class
There are some unusual aspects of constructor
functions.
 First, it is no accident that they have exactly the same
 This is one way the compiler knows they are
constructors.
 Second, no return type is used for constructors. Why
not?
 Since the constructor is called automatically by the
system, there’s no program for it to return anything
 Return value wouldn’t make sense. This is the second
way the compiler knows they are constructors.
Rectangle Example with object
Rectangle Example with constructor
Example 3: Output?
Declaring member function outside the class

You might also like