Constructors in c++ language

Published:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Constructors:

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

The name of the constructor is same as the class name. The constructor is invoked whenever an object of its associated class is created.

Constructors should be declared in the public section, they do not have return types and they cannot be virtual.

Program:

#include<iostream.h>

class integer

{

int m,n;

public:

integer(int,int);

void display(void)

{

cout<< "m= "<< m << "\n";

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

}

};

Integer::integer(int x,int y)

{

m=x;

n=y;

}

Int main()

{

integer int(10,20);

int.display();

}

Output:

m=10;

n=20;

Destructors:

It is used to destroy the objects that have been created by a constructor.

Program:

#include<iostream.h>

int count=0;

class alfa

{

public:

alfa()

{

count++;

cout<<"\n no.of obj created"<<count;

}

~alfa()

{

cout<<"\n no.of obj destroyed"<<count;

count--;

}

};

int main()

{

cout<<"\n enter main\n";

alfa a1,a2;

{

cout<< "\nenter block1\n";

alfa a3;

}

{

cout<<"\nenter block2\n";

alfa a4;

}

cout<<"\nre-enter main\n";

}

Output:

Enter main

No of obj created 1

No of obj created 2

Enter block1

No of obj created 3;

No of obj destroyed 3;

Enter block2

No of obj created 3;

No of obj destroyed 3;

Re-enter main

No of obj destroyed 2

No of obj destroyed 1

Public, private and protected access specifiers:

An access specifier specifies the accessibility of members that follow it until the next access specifier or until the end of the class definition. You can use any number of access specifiers in any order. The access specifier determines how accessible the field is

to code in other classes. Access ranges from totally accessible to totally inaccessible. You can optionally declare a field with an access specifier keyword: public, private, or protected.

* Public: can be used anywhere without the access restrictions defined by private or protected.

* Private: can be used only by the members and friends of class .

* protected: can be used only by the members and friends of class , and the members and friends of classes derived from class.

Member data and member functions:

The class body contains the declaration of variables and functions. These functions and variables are collectively called class members.

Class members which are declared as private can be accessed only from within the class, whereas public members can be accessed from outside the class also.

The variables declared inside the class are known as data members and functions known as member functions. Only the member functions can have access to the private data members and private functions. He binding of data and functions together into a single class-type variable is referred to as encapsulation.

Copy constructor:

A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.

Normally the compiler automatically creates a copy constructor for each class (known as a default copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one.

Program:

#include <iostream>

class Person

{

public:

int age;

Person(int age)

};

int main()

{

Person timmy(10);

Person sally(15);

Person timmy_clone = timmy;

cout << timmy.age << " " << sally.age << " " << timmy_clone.age ;

timmy.age = 23;

cout << timmy.age << " " << sally.age << " " << timmy_clone.age;

}

Output:

10 15 10

23 15 10

Friend Function:

A friend function is a function that is not a member of a class but has access to the class's private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges. Friends are not in the class's scope, and they are not called using the member-selection operators (. and ->) unless they are members of another class. A friend function is declared by the class that is granting access. The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.

Program:

#include<iostream.h>

Class sample

{

int a;

int b;

public:

void setvalue()

{

a=25;

b=30;

}

friend float mean(sample s);

};

float mean(sample s)

{

return float(s.a+s.b)/2.0;

}

int main()

{

Sample z;

z.setvalue();

cout<<"mean value = "<<mean(z)<<"\n";

return 0;

}

Output:

Mean value=27.5

Overloaded operators:

Operator overloading means assigning different meanings to an operator. C++ permits overloading of operators, thus allowing us to assign multiple meanings to operators. The number and type of the operands decide the nature of operation to follow.

Program:

#include<iostream.h>

class space

{

int x;

int y;

int z;

public:

void getdata(int a, int b, int c);

void display(void);

void operator-(); //overload unary minus

};

void space :: getdata(int a, int b, int c)

{

x=a;

y=b;

z=c;

}

void space :: display(void)

{

cout<< x <<" ";

cout<< y <<" ";

cout<< z <<" ";

}

void space :: operator~()

{

x=-x;

y=-y;

z=-z;

}

Int main()

{

space s;

s.getdata(10, -20, 30);

cout<<"s :";

s.display();

-s; //activates operator-() function

Cout<<"s :";

s.display():

return 0;

}

Output:

s: 10 -20 30

s: -10 20 30

Inline Function:

An Inline function is a function that is expanded in line when it is invoked. That is, the compiler replaces the function call with the corresponding function code.

Program:

#include<iostream.h>

inline float mul( float x, float y)

{

return(x*y);

}

inline double div(double p, double q)

{

return(p/q);

}

int main()

{

float a=12.345;

float b=9.82;

cout<<mul(a,b)<<"/n";

cout<<div(a,b)<<"/n";

}

Output:

121.228

1.25713

Pointers:

Pointer is a derived data type that refers to another data variable by storing the variables memory address rather than data. Pointer variable can also refer to another pointer in c++.

Program:

#include<iostream.h>

#include<conio.h>

Void main()

{

int a=10, *ptr;

ptr=&a;

cout<<"value of a is:"<<a;

*ptr=(*ptr)/2;

cout<<"value of a is:"<<(*ptr);

}

Output:

Value of a is:10

Value of a is:5

Writing Services

Essay Writing
Service

Find out how the very best essay writing service can help you accomplish more and achieve higher marks today.

Assignment Writing Service

From complicated assignments to tricky tasks, our experts can tackle virtually any question thrown at them.

Dissertation Writing Service

A dissertation (also known as a thesis or research project) is probably the most important piece of work for any student! From full dissertations to individual chapters, we’re on hand to support you.

Coursework Writing Service

Our expert qualified writers can help you get your coursework right first time, every time.

Dissertation Proposal Service

The first step to completing a dissertation is to create a proposal that talks about what you wish to do. Our experts can design suitable methodologies - perfect to help you get started with a dissertation.

Report Writing
Service

Reports for any audience. Perfectly structured, professionally written, and tailored to suit your exact requirements.

Essay Skeleton Answer Service

If you’re just looking for some help to get started on an essay, our outline service provides you with a perfect essay plan.

Marking & Proofreading Service

Not sure if your work is hitting the mark? Struggling to get feedback from your lecturer? Our premium marking service was created just for you - get the feedback you deserve now.

Exam Revision
Service

Exams can be one of the most stressful experiences you’ll ever have! Revision is key, and we’re here to help. With custom created revision notes and exam answers, you’ll never feel underprepared again.