Constructor Overloading Notes C++, Lecture notes of Object Oriented Programming

This notes describes complete constructor overloading and its types. This is implemented in C++. You can use this concept with the language of your interest.

Typology: Lecture notes

2018/2019

Uploaded on 07/22/2019

asif-ameer
asif-ameer 🇵🇰

1 document

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Constructors/ Destructor
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Constructor Overloading Notes C++ and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Constructors/ Destructor

Constructors

  • Member functions can be used to give values to the data items in an object.

Sometimes, however, it’s convenient if an object can initialize itself when it’s first

created, without requiring a separate call to a member function.

  • A type of member function that is automatically executed when an object of the

class is created is known as constructor.

  • The constructor has
    • no return type and
    • has the same name that of class name.
  • The constructor can work as normal function but it cannot return any value.
  • It is normally defined in classes to initialize data members.
  • It construct the object, therefore it is called constructor.

Example

#include <iostream.h>

#include <conio.h>

class Hello

private:

int n;

public:

Hello()

{ cout<<”Object is created”; }

void main()

Hello x,y,z;

getch();

In this program, three objects are

created, so the constructor will be

called three times and display

OUTPUT

Object is created

Object is created

Object is created

Same Name as the Class, No Return Type

  • There are some unusual aspects of constructor functions.
    • First, it is no accident that they have exactly the same name as the class of

which they are members.

  • 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 to; a return value wouldn’t make sense.
  • These are two ways, the compiler knows that they are constructors.

Example

#include <iostream.h> #include <conio.h> class Number { private: int n,m; public: Number() //Default Constructor Explicit Definition { n = m = 100; } Avg() { cout<<“Average of two numbers=”<<(n+m)/2; } }; void main() { Number x; x.avg(); getch(); }

Passing Parameters to Constructor

  • Constructor can have parameters as normal functions do.
  • The method of passing the parameters to a constructor is same as passing parameters to a normal function.
  • The only difference is that parameters are passed to the constructor when the object is declared.
  • The parameters are written in parenthesis along with the object name in declaration statement.
  • Syntax (parameterized constructor): Constructor_Name(parameter_list) { Constructor body; }
  • Syntax (creating object): Class_Name object_name(parameter_list);

Default Copy Constructor

  • A type of constructor that is used to initialize an object with another object of the same type is known as

default copy constructor or copy constructor.

  • Its name is “default copy constructor” because it is available by default in all classes. The user does not need

to write this constructor.

  • It accepts a single object of the same type as parameter.
    • The parameter for default copy constructor can be given in parenthesis or using assignment operator.
  • Syntax

class_name object_name (parameter); OR

class_name object_name = parameter;

  • class_name
    • is the name of class and indicates the type of object to be created.
  • Object_name
    • indicates the name of object to be created.
  • Parameter
    • indicates the name of parameter that is passed to default copy constructor. The values of data members of parameter object are copied to the data members of the new object. The type of new object and parameter object must be same.

Constructor Overloading

  • The process of declaring multiple constructors with same name but different parameters is known as constructor overloading.
  • The constructors with same name must differ in one of the following ways: - Number of parameters - Type of parameter - Sequence of parameters
  • It’s convenient to be able to give values to member variables of a class when the object is created.

Destructors

  • We’ve seen that a special member function—the constructor—is called automatically when an object is first created.
  • You might guess that another function is called automatically when an object is destroyed. Such a function is called a destructor.
  • A destructor has
    • same name as the constructor or class name
    • No return type
    • Not accept any parameters.
    • preceded by a tilde sign ( ~ ).
  • Syntax ~ name() { Destructor body }

Example

#include<iostream.h> #include<conio.h> class Foo { private: int data; public: Foo() //constructor { cout<<”Object Created”<<endl; } ~Foo() //destructor { cout<<”Object Destroyed”<<endl; } }; int main() { Foo f1,f2; return 0; }

  • OUTPUT:

Object Created

Object Created

Object Destroyed

Object Destroyed