You are on page 1of 6

Intensive Programming Lab 4

Introduction to Classes-Part II
(Constructors)
Lab Objectives:
1. Understand the concept of constructors
2. Learn and practice types of constructors
3. Learn Constructor Overloading

Software Required:
Dev C++
Constructors
Constructors are special class member functions which perform initialization of every object. The
Compiler calls the Constructor whenever an object is created. Constructors initialize values to object
members after storage is allocated to the object.

A constructor is different from normal functions in following ways:


 Constructor has same name as the class itself
 Constructors don’t have return type
 A constructor is automatically called when an object is created.
 If we do not specify a constructor, C++ compiler generates a default constructor for us
(expects no parameters and has an empty body).
Syntax
/*.....class with constructor..........*/
class class_name
{
.........
public:
class_name(); //constructor declared or constructor prototype
.........
};
class_name :: class_name() //constructor defined
Intensive Programming Lab 4

Types of Constructors

Constructors are of three types:


1. Default Constructor
2. Parametrized Constructor
3. Copy Constructor
Default Constructor
Default constructor is the constructor which doesn't take any argument. It has no parameter.
Syntax :

class_name ()
{ Constructor Definition }

Example 1

#include <iostream>
using namespace
std; class construct
{ public:
int a, b;
construct( // Default
) Constructor
{
a = 10;
b = 20; } };
int main() {

construct c;// Default constructor called automatically when the object is created cout
<< "a: "<< c.a << endl << "b: "<< c.b;
return 1;
}

Parametrized Constructor
Intensive Programming Lab 4

These are the constructors with parameter. Using this Constructor you can provide different values to
data members of different objects, by passing the appropriate values as argument.

Example 2
#include<iostream>

using namespace std;

class Point {

int x, y;

public:

Point(int x1, int y1) // Parameterized Constructor

x = x1;

y = y1;

int getX() {

return x;

int getY() {

return y;

} };

int main() {

Point p1(10, 15); // Constructor called

// Access values assigned by constructor

cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

return 0;

}
Intensive Programming Lab 4

Constructor Overloading
Every constructor has same name as class name but they differ in terms of either number of
arguments or the datatypes of the arguments or the both.

As there is more than one constructor in class it is also called multiple constructors.

Example 3

#include <iostream>
using namespace std;
class ABC {
private: int x,y;
public:
ABC () //constructor 1 with no arguments
{
x = y = 0;
}
ABC(int a) //constructor 2 with one argument
{
x = y = a;
}
ABC(int a,int b) //constructor 3 with two argument
{
x = a; y = b;
}
void display() {
cout << "x = " << x << " and " << "y = " << y << endl;
} };

int main() {
ABC cc1; //constructor 1 ABC cc2(10); //constructor 2
ABC cc3(10,20); //constructor 3
cc1.display();
cc2.display();
cc3.display(); return 0;
}
Intensive Programming Lab 4

Practice Exercise

Q1: Write a C++ program that input two integers in main function and pass them to default
constructor of the class Add. Show the result of the addition of two numbers using a member function
named addition.

Q2: Write a c++ program for matrix multiplication with following specifications
a. Use constructor to input the no. of rows and no. of columns for matrix
b. Use getdata() function to input values for matrix
c. Use show() to display the matrix
d. Use mul() to multiply two matrices
Pseudo code:

matrix
r,c
matrix()
void getdata()
void show()
mul()

Testing

Test case for Example 1

Sample Inputs Sample Outputs


…………. a: 10
b: 20

Test
case for Example 2

Sample Inputs Sample Outputs

…………. p1.x = 10, p1.y = 15


Intensive Programming Lab 4

Test case for Example 3

Sample Inputs Sample Outputs

…………. x = 0 and y = 0
x = 10 and y = 10
x= 10 and y = 20
Test
case for Practice Q1

Sample Inputs and Outputs

Enter first number : 3


Enter second number : 4
numbers initialized
The addition result on:7

Test case for Practice Q2

Sample Inputs and Outputs

Program for calculation of Matrix


Multiplication enter no. of rows 2
enter no. of columns 2
Enter data for first
array: 1
2
3
4
Enter data for second array:
6
7
8
9
Output Matrix:
22 25
50 57

You might also like