***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Monday, April 26, 2021

TYPES OF CONSTRUCTORS

The constructors in C++ can be classified as

  •  Default Constructors     
  • Parameterized Constructors   
  • Copy Constructors

1. Default Constructors

A constructor that does not take any parameter (argument) is called a default constructor. It simply allocates storage for the data members.

Ex.                    

//Demonstration on Default constructor

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample()

{ n=100; }

void show()

{

cout<<"\n N= "<<n;              

}                                                                                                                        

};                                                                                                                                                  

void main()                                                                                                                                   

{                                                                                                            

sample s;                                                                                                                                       

s.show();                                                                                                                                       

}                                                                                                                                                   

2. Parameterized Constructors:-

A constructor that is defined with parameters is called a parameterized constructor. It is used to initialize the data members of a class.

For a parameterized constructor, the data will be sending through that class object declaration only, because constructors are executed by the object declaration.

Ex. //Demonstration on parameterized constructor

#include<iostream.h>

#include<conio.h> class sample

{

private: int n; public:

sample(int k)

{

 n=k;

}

void show()

{

cout<<"\n N= "<<n;

}

};                                                            

void main()                                             

{                                                             

clrscr();

sample s1(50);

s1.show();

getch();

}

3. Copy Constructors

It is used to copy the data values of one object into the members of another object. This constructor takes an object of the class as an argument.

It takes only one argument, also known as one argument constructor.

It creates a new object from an existing one by initialization. A copy constructor takes a reference to an object of the same class as an argument.

Ex.

//Demonstration on copy constructor

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample(int k)

{

 n=k;

}

sample(sample &s)

{

n=s.n;

}

void show()

{

cout<<"\n N= "<<n;

}

};                                                                                                                                  

void main()                                                                                                                   

{                                                                                                                                   

clrscr();

sample s1(50);                                                                                                              

s1.show();                                                                       

sample s2(s1);                                                                                                              

s2.show();                                                                          

getch();                                                                                                                         

}

CONSTRUCTOR

C++ provides a special member function called the constructor which enables an object to initialize itself when it is created. This is known as automatic initialization of objects.

Similarly, it also provides another member function called the destructor that destroys the objects when they are not required.

CONSTRUCTOR

A constructor is a special member function of a class, which constructs an object.

A constructor must be declared in public specifier of a class. It is mainly used to initialize the data members of a class.

A constructor name is as same name as the class name. The constructors should not have any return type

The constructors are defined with or without arguments

A constructor is an automatically executed member function of a class. A constructor is invoked when the class object is instantiated (created).

A constructor is invoked once for each object.

A constructor is also defined inside/outside of a class.

Syntax              

public:

classname ( [parameter_list] )

{

Statement block;

}

Example:

class sample

{

private:

int n;

public:

sample()

{

n=0;

}

sample(int k) {

n=k;

 }

};

Friend functions and Friend classes

Data hiding is the fundamental concept of object-oriented programming. It means a class private or protected data can access within that class or that class members only.

A class private or protected data cannot be accessed in outside the class or another class. To access the private data in outside class or another class, C++ provides the feature called friend functions and friend classes.

Friend Functions

  • In general, class private or protected members are not accessed in outside of the class or another class.
  • To access the private members of a class in outside the class, C++ provides the feature called “Friend functions‟.
  • A friend function is not a member function of a class, that is, it is a non-member function of the class. But it can access the private members of a class.
  • A friend function is declared inside the public specifier of the class and is defined (implemented) outside the class as an ordinary function.
  • A friend function is identified by the keyword "friend" followed by function declaration.
  • A friend function is declared within a class.

Syntax for a friend function is

class class_name

{

Public:

friend return_type fun_name(args_list);

………

};

return_type fun_name(args_list)

{

statements;

}

}


//Demonstration on friend function

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int n;

public:

sample()

{

n=0;

}

sample(int k) {

n=k;

 }

void show(char *msg)

{

cout<<"\n"<<msg<<n;

}

friend int add(sample, sample);

};

int add(sample x, sample y)

{

return x.n+y.n;

}

void main()

{

clrscr();

sample s1(10),s2(40);

s1.show("s1 : ");

s2.show("s2 : ");

cout<<"\n s1+s2 = "<<add(s1,s2); getch();

}


Friend Class

·       In general, class private or protected members cannot be accessed by the members of another class. To access those members, C++ provides the “friend classes‟.

·         A friend class can access the private members of another class.

·         To declare a friend class, use the keyword “friend‟.

Syntax:-       

friend class classname;

·         A friend class is declared in another class for which it is to be friend.

Ex.               

class A

{

friend class B;

………

};

class B

{

………

.…….

};

·         Similar to friend functions, a class can be friend to any number of classes.

·         When you declare a class as friend, then all its members also became the friend of the other class.A friendship must always be explicitly specified (defined).A friendship is not transitive.

Ex. //Adding two objects of a class using friend function

#include<iostream.h>

 #include<conio.h> 

class first

{

private: int n; public:

first()

{

n=0;                  

}

first(int k)

{                       

n=k;                  

}

void show(char *msg)

{

cout<<"\n"<<msg<<n;

}

friend class second;

};

class second

{

public:

int add(first x, first y)

{

return x.n+y.n;

}

};

void main()

{

clrscr();

first f1(10),f2(40);

f1.show("f1 : ");

f2.show("f2 : ");

second s;

cout<<"\nf1+f2 = "<<s.add(f1,f2); 

getch();

}


Static Data Members - Static Member Functions

Static Data Members

C++ permits programmers to define static data members within a class. If a data member is preceded by the static keyword is called static data member. Irrespective of the number of objects created, only a single copy of the static member is created in memory.

For a static data member the memory is allocated only once along with the class definition is loaded, but for a data member, the memory is allocated for each object.

When a data member is declared as static, the following must be noted.

  • All objects of a class share the static members.
  • All static data members are initialized to zero when the first object of the class is created.
  • Static data members are visible only within the class but their lifetime is the entire program.
  • Static data members are usually used to maintain values that are common for the all the objects
  • Static data members are declared inside a class, they are not considered to be a part of the objects.
  • A static data member has a file scope.
  • A static data member is declared inside the class, they can be accessed only by using the class name and the scope resolution operator.
  • A static data member is initialized in outside the class using the class name and scope resolution operator.

Syntax:                                   

class classname

{                                                                                            

private:

static datatype variable;

----------                                   

public:                                                                                   

-------                                       

};                                                                                           

datatype classname : : variable = value;

Example:


Static Member Functions

C++ not only allows data members of a class to be static but also supports static member functions. Any static member; either data or function - is allocated memory only once along with class definition.

Static member is not a part of any object, but it is shared by all the objects of that class.

A static member function has the following features.

          A static member function can access only the static members (data and/or functions) declared in the same class.

          It cannot access non-static members because they belong to an object.

          A static member is called using the class name and the scope resolution operator.

          A static member function is preceded by the keyword “static‟, followed by the function definition.

Syntax:-

static return_type fun_name([args_list])

{

-------------

}

A static member function can be called using the class name, but not with the object. To invoke a static member function scope resolution operator is used.

Syntax:-           

classname :: fun_name(args);

Example:

#include<iostream.h>

#include<conio.h>

class id_generator

{

private:

static int next_id;

public:

static void gen_next_id()

{

cout<<"\nNEXT ID= "<<next_id++;

}

};

int id_generator::next_id=1;

void main()

{

clrscr();

for(int i=0;i<5;i++)

id_generator::gen_next_id();

getch();

}

 Every time the static function is called, it increments the value of static variable by 1.

 In this program we have not made any object of the class. This is because our class has only one member function and that is static.

Static member functions are not invoked by objects. They are called using the class name and scope resolution operator.