C++ Notes (1-07-2021)
C++ Notes (1-07-2021)
Explain
Ans:The specification of a class can be separated from its implementation by placing the
class declaration in a header file. The definitions of a member function are stored in .cpp
files of their own. The header file which contains the declaration of the class is called class
specification file and it has .h extension. The .cpp file which stores the member function
definitions is called class implementation file. The class header file must be preceded with
#include and .cpp file must be compiled and linked only with the main program.
Square.h
#ifndef SQUAR_H
#define SQUARE_H
class square
{
private:
double side;
public:
void setSide(double);
double getSide() const;
double getArea() const;
};
Square.cpp
#include "square.h"
#include<iostream.h>
#include<cstdlib.h>
using namespace std;
void square:: setSide(double S);
{
if(S>=0)
side=S;
else
{
cout<"Invalid side\n";
exit(EXIT_FAILURE);
}
}
double square::getSide() const
{
return side;
}
double square::getArea() const
{
return side*side;
}
Square_main.cpp
#include<iostream.h>
#include "square.h"
using namespace std;
int main()
{
square box;
double sqSide;
cout<<"This program will calculate the area of a square\n";
cout<<"What is the side of the square?\n”
cin>>sqSide;
box.setSide(sqSide);
cout<<"Here is the squares data:\n";
cout<<"Side:"<<box.getSide()<<endl;
cout<<"Area:"<<box.getArea()<<endl;
return 0;
}
Q.Define constructor. Explain various types of constructors in C++.
Ans: Constructor
A constructor is a member function which automatically gets called whenever the object of
the class is instantiated. It does not return any value.
Syntax:
class integer
{
int m,n;
public:
integer(void);
}
integer::integer(void)
{
m=0;
n=0;
}
Types of constructors
1. Default constructor
2. Parameterized constructor
3. Copy constructor
4. Constructor with default arguments
Default Constructor
A constructor that does not take any argument is known as default constructor. If no
constructor is specified then a default constructor is called automatically. It does not accept
any argument.
Program
#include<iostream.h>
#include<conio.h>
class number
{
private:
int x;
float y;
public:
number();
void display()
{
cout<<"\n\t x="<<x<<"\t y="<<y;
}
};
number::number()
{
cout<<"\n Constructor containing no arguments:";
x=y=NULL;
}
int main()
{
class number C;
C.display();
getch();
return 0;
}
Parameterized constructor
A constructor that takes arguments as its parameters is known as parameterized
constructor. It is used to initialize the elements that take different values. Whenever objects
are declared then their initial values are passed as arguments to constructor function. This
can be done using following two methord
(i) Explicit constructor calling
(ii) Implicit constructor calling
(a)Explicit constructor calling
In this method, the constructor is called explicitly by means of writing its name and passing
arguments ( if any).
Syntax
class_name object_name=constructor_name(arg1, arg2);
eg: point obj1=point(10,15)
(b)Implicit constructor calling
In this method, the constructor is called implicitly by means of simply writing the class name
followed by object name and passing parameters without using the name of the
constructor.
Syntax
class_name object_name(arg1,arg2);
eg: point obj1(10,15)
Program
#include<iostream.h>
#include<conio.h>
class mylcass
{
int x,y;
public:
myclass(int p, int q)
{
x=p;3
y=q;5
}
void show()
{
cout<<”x=”<<x<<""<<”y=”<<y;
}
};
int main()
{
myclass mc(3,5);
mc.show();
getch();
return 0;
}
Output
X=3 y=5
Copy constructor
Copy constructor is a constructor function used for copying objects. It has the same name as
that of a class and takes a reference to a constant parameter.
This constructor copies one object from the other sequentially during the object
declaration. This process of initializing is called copy initialization.
Syntax:
Class A
{
………..
………..
Public:
A(A &c);
Program
#include<iostream.h>
#include<conio.h>
class Example
{
private:
int data;
public:
Example()
{
}
Example(int a)
{
data=a;
}
Example(Example &c)
{
data=c.data;
cout<<"Copy constructor invoked";
}
void show()
{
cout<<data;
}
};
main()
{
clrscr();
Example e(20);
e.show();
Example e1;
e1=e;
e1.show();
getch();
return 0;
}
Output
20
20
Constructors with default arguments
C++ allows the programmer to invoke a constructor without specifying all of its arguments.
In such situation, the constructor assigns a default value to the argument that doesn’t have
a matching value. The assignment of default value is done during function declaration.
Generally, a default argument is checked for type during its declaration and evaluated at the
time of its declaration.
Program
#include<conio.h>
#include<iostream.h>
class student
{
int rollno;
float marks;
public:
student(int p=10, float q=40)
{
rollno=p;10
marks=q;40
}
void show()
{
cout<<"Roll no"<<rollno<<endl;
cout<<"Marks"<<marks<<endl;
}
};
void main()
{
student x;
student y(1,50);
clrscr();
cout<<"Output using default constructor arguments:\n";
x.show();
cout<<"Output without default constructor arguments:\n";
y.show();
getch();
}