0% found this document useful (0 votes)
3 views55 pages

C++ Constructor

The document explains pointers in C++, detailing their syntax and usage, including reference and dereference operators. It also covers function parameters, constructors (default, parameterized, and copy), static members, friend classes and functions, virtual base classes, and virtual functions. Additionally, it discusses type conversion and file handling in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views55 pages

C++ Constructor

The document explains pointers in C++, detailing their syntax and usage, including reference and dereference operators. It also covers function parameters, constructors (default, parameterized, and copy), static members, friend classes and functions, virtual base classes, and virtual functions. Additionally, it discusses type conversion and file handling in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Pointer in C++

In C++, a pointer refers to a variable that holds


the address of another variable. Like regular
variables, pointers have a data type. For
example, a pointer of type integer can hold the
address of a variable of type integer. A pointer
of character type can hold the address of a
variable of character type.
Syntax:
datatype *variable_name;
The reference operator (&) returns the variable’s address.
The dereference operator (*) helps us get the value that has
been stored in a memory address.

int *x; // a pointer to integer


double *x; // a pointer to double
float *x; // a pointer to float
char *ch // a pointer to a character
Example
Void main()
{
int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl;
}
Function with Parameters

Parameters refer to the values that are


declared inside the parentheses. It passes
these values to the function, and a comma
separates each parameter from the other
parameter. For example, if there is a function
for adding or subtracting two numbers, then
that function must have two parameters.
Types of Parameters
• Formal parameters: parameters
those are passed during the
declaration/ definition of a function.
• Actual parameters: parameters
those are passed while calling the
function.
Call by Value is a method in which it passes the copies of actual
parameters to the formal parameters inside the function. And as you
know, it passes the copies, so even if there is any change in the
values inside the function, it will not reflect that change in the actual
values.
Here copies of the values num1 and num2 are
being passed to n1 and n2. The actual parameters
are num1, num2, and it passes the copies of the
values to the function's formal parameters.
Call by Reference is a method in which it passes the
reference or address of the actual parameter to the
function's formal parameters, which means if there
is any change in the values inside the function, it
reflects that change in the actual values.
Here &n1 and &n2 are the reference or
addresses to num1 and num2, and that is
why the change in the formal parameter
will be reflected in the actual parameter.
Call by valure Call by reference

• In this method, it passes the value of • In this method, it passes the


the variable as an argument. address of the variable as an
argument.

• In this method, the actual value is not • In this method, the actual value is
modified.
modified.

• This method is slow. • This method is fast.

• Both formal and actual arguments


• Both formal and actual parameters are at the same memory location
have different memory locations.
Constructor
• A constructor is a special type of
member function that is called
automatically when an object is
created.
• In C++, a constructor has the same
name as that of the class and it does
not have a return type.
Types of Constructors in C++

There are 3 types of constructors in C+


+, They are :
• Default Constructor
• Parameterized Constructor
• Copy Constructor
Default constructor
A constructor to which no arguments
are passed is called the Default
constructor. It is also called a
constructor with no parameters.
{ public: int size; //default constructor
Line()
{
size=30;
}
};
int main()
{ //default constructor called when object is
created
Line l;
cout<<"Line size is"<<" "<<l.size<<"
"<<"cm";
return 0;
}
parameterized
constructor
When an object is created, the
parameterized constructors can accept
arguments to initialise it. Similar to
adding parameters to a regular
function, parameters are added to a
parameterized constructor
class ABC
{ private: int x,y;
public:
ABC () //constructor 1 with no arguments
{ x = y = 0;
}
ABC(int a) //constructor 2 with one argument { x = y = a; }
ABC(int a,int b) //constructor 3 with two argument
{ x = a; y = b; }
void display()
{ cout << "x = " << x << " and " << "y = " << y << endl;
}
};
int main()
{ ABC cc1; //constructor 1
ABC cc2(10); //constructor 2
ABC cc3(10,20); //constructor 3
cc1.display();
cc2.display();
cc3.display();
Copy Constructor
• A copy constructor is a member function that
initializes an object using another object of the same
class. In simple terms, a constructor which creates an
object by initializing it with an object of the same
class, which has been created previously is known as
a copy constructor.
• Copy constructor is used to initialize the members of a
newly created object by copying the members of an
already existing object.

• Copy constructor takes a reference to an object of the


same class as an argument.
.
// declare a class
class Wall {
private:
double length;

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:

// initialize variables with parameterized constructor


Wall(double len, double hgt) {
length = len;
height = hgt;
}

// copy constructor with a Wall object as parameter


// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}

double calculateArea() {
return length * height;
}
};

int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);

// copy contents of wall1 to wall2


Wall wall2 = wall1;

// print areas of wall1 and wall2


cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();

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
{

private: int number;

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

Built-in type Class type to One class to


to class type built-in type another class
Type Conversion

When constants and variables of different types are mixed in an


expression, then compiler applies automatic type conversion.
Example:
int x;
float y;
y = 12.239;
x = y;
it converts y to an integer.
Y= 12
File Handling
Example

• #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();
}

You might also like