C++ Constructor
C++ Constructor
• In this method, the actual value is not • In this method, the actual value is
modified.
modified.
public:
// default constructor to initialize variable
Wall() {
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
Wall wall1;
return 0;
}
// C++ program to calculate the area of a wall
// declare a class
class Wall {
private:
double length;
double height;
public:
// parameterized constructor to initialize variables
Wall(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() {
return length * height;
}
};
int main() {
rea of Wall 1: 90.3
Area of Wall 2: 53.55
he copy constructor in C++ is used to copy data of one object to
another.
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
return 0;
Area of Wall 1: 90.3
Area of Wall 2: 90.3
Area of Wall 1:
90.3
Area of Wall: 90.3
Static Members
Static Data Member
Static data member is just like a global variable. It means
static data member is globally accessible by all the object
of that class type. The static data member are usually
maintained to store values common to the entire class.
Two things are needed for making data member static:
• Declaration within the class definition
•Definition outside the class definition
Example:
Class X
{
Static int count; // declaration within the class definition
------------
--------------
};
Int x::count; //definition outside the class definition
Static Member Function
A static Member function is a function that can only access
static data member. Function can be static by putting keyword
static before the function declaration in the class definition.
Example:
Class X
{
static int count;
static void show()
{
cout <<count<<“\n”;
}
};
X::show()
Example
# include<iostream.h>
#include<conio.h>
Class staticTest
{
private:
static int x;
Public:
static void count()
{ cout<<x<<“\n”;
}
};
int staticTest :: x=9;
void main()
{
Cout<<staticTest :: count();
Friend class
Friend class will have access to all private member of the other class. We can make one
class to be a friend of another class. A friend class has full access of private data
member of another class without being member of that calss.
Syntax:
class X
{
private:
----------------
public: function_name()
{
--------
}
friend class classs_name;
};
Friend function
Friend function is used for accessing the non –
public member of a class.
Normally, a function that is not a member of a
class can not access such information.
Friend function is used in OOP to allow access
to private or protected data in class from the
outside.
Syntax: friend return_type function(argument);
EXAMPLE:
Class A
{
public: void A ()
{
number = 200;
}
friend class B;
};
Class B
{
public: void friendclass( A aobj)
{
cout<<“value of data is <<aobj.number;
}
};
Void main()
{
A obj1;
B obj;
Obj.friendclass(obj1);
}
Output is:
valueof data is 9
Data= 200
Virtual Base class
Virtual as the name suggests, it is something that
exists in effect but not in reality.
Virtual Base class is used to prevent multiple copies
of Base class from being created during the
declaration of 3 rd Derived class. If Derived tries
to reference a variable in Base,an error will occur.
Example
Base
Derived1 Derived2
Derived3
Example
Class Base
{
int a;
-------
};
Class Derived1: virtual public Base
{
int b;
-------
};
Class Derived2: virtual public Base
{
int c;
-------
};
Class Derived3: public Derived1,public Derived2
{
int d;
-------
Virtual Function
Virtual Function is a mechanism to implement the concept
of polymorphism. Polymorphism is the ability to give
different meaning to one function. virtual keyword is used
to declare virtual function.
Syntax :
class Base_Class_name
{
public:
virtual void function_name() // virtual function
{
--------
}
};
Type conversion
Data
conversion
• #include<iostream>
• #include <fstream>
• void main()
• {
• fstream new_file;
• new_file.open("new_file",ios::out);
• if(!new_file)
• {
• cout<<"File creation failed";
• }
• else
• {
• cout<<"New file created";
• new_file.close();
Built-in to class type
class time
{
Public:
int hr, min;
public:
--------
Time(int t) // constructor
{
hr = t/60; // t in minutes
min = t% 60;
}
};
Time T1;
int duration = 90;
T1 = duration; //int to class type
After this conversion , hr member of T1 will contain a value of 1 and min contain 30
Class to Basic
Time :: operator int()
{
int min1= hr * 60;
min1 = min1+min;
return min1;
}
Time T1;
int m = T1; // class to basic
One class to another class type
obj A = obj B
Destination Source
Example:
Class vehicle //This denotes base class of C++ virtual function
{
public:
virtual void make() //virtual function
{
cout <<“Member function of Base class vihicle accessed”;
}
};
Class Four Wheeler:public vehicle
{
public:
void make()
{
cout <<“virtual Member function of Derived class fou Wheeler accessed”;
}
};
Void main()
{
Vihicle *a,*b;
a=new vihicle;
a-> make()
b=new fourWheeler;
b->make();
}