Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

UNIT-2: Classes and Object, Dynamic Constructor & Destructor BCA-2 Sem

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

UNIT-2

Classes and Object, Dynamic


Constructor & Destructor
BCA-2nd Sem
Class
• A class in C++ is the building block, that leads to
Object-Oriented programming
• It is a user-defined data type, which holds its own
data members and member functions, which can be
accessed and used by creating an instance of that
class
• C++ class is like a blueprint for an object
• Example: Class of cars
Object
• An Object is an instance of a Class.
• When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created)
memory is allocated
Example
• An Object is an instance of a Class
• When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created)
memory is allocated
Declaring class and objects
• The body of class is defined inside the curly
brackets and terminated by a semicolon at the end
Declaring object
• When a class is defined, only the specification for
the object is defined, no memory or storage is
allocated
• To use the data and access functions defined in the
class, you need to create objects.
• Syntax:
ClassName ObjectName;
Member Functions
• The program defines person as a new data of type
class

• The class person include two basic data type items


and two function to operate on that data. it’s called
Member function.
Data Member

• Data member depends on access control of data


member
• If it’s public then data member can be easily accessed
using the direct member access operator with the
object of that class
• If the data member is defined as private or protected
then we can not access the data variables directly.
Data Member
Type of data Member

1.Public

2.Private

3.protected
Data member type
#include <iostream>
using namespace std;
class alpha{
private:
int id;
static int count;
public:
alpha(){count++;
id=count;}}
Data member type
void print(){
cout<<"My id is"<<id;
cout<<"countis"<<count;} };
int alpha : :count=0;
void main (){
alpha a1,a2,a3;
a1.print();
a2.print();
a3.print();}
Array of object
class A
{
int* myArray;
A()
{
myArray = 0;
}
A(int size)
{
Array of object
myArray = new int[size];
}
~A()
{
delete [] myArray;
}
}
Returning Objects from Functions

#include <iostream>
class Complex{
private: int real;
int imag;
public: Complex():
real(0), imag(0){ }
void Read() {
// Constructor with two parameters
construct(int a, int b)
{
area = a * b;
}

void disp()
{
cout<< area<< endl;
}
};
Continue…
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
int main()
{
Continue…
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3=c1.Add(c2);
c3.Display();
return 0; }
Constructor
• A constructor in C++ is a special method that is
automatically called when an object of a class is
created

• A constructor (having the same name as that of the


class) is a member function which is automatically
used to initialize the objects of the class type with
legal initial values.
Characteristics of Constructor

(i) These are called automatically when the objects are


created

(ii) All objects of the class having a constructor are


initialized before some use

(iii)These should be declared in the public section for


availability to all the functions

(iv)Return type (not even void) cannot be specified for


constructors
Continue…..
(iv)These cannot be inherited, but a derived class can
call the base class constructor

(v) These cannot be static

(vii)Default & copy constructors are generated by the


compiler wherever required. Generated constructors
are public

(viii)These can have default arguments as other C++


functions
Continue….
(vii)A constructor can call member functions of its class

(x)An object of a class with a constructor cannot be


used as a member of a union

(xi) A constructor can call member functions of its class

(xii)We can use a constructor to create new objects of its


class type by using the syntax
Types of Constructor
1. Overloaded Constructors

2. Dynamic constructor

3. Copy Constructor

4. Default Constructor
1.Overloaded Constructors

• Besides performing the role of member data


initialization, constructors are no different from other
functions.

• This included overloading also. In fact, it is very


common to find overloaded constructors.

• For example, consider the following program with


overloaded constructors for the figure class :
2. Dynamic Constructor
• Dynamic constructor is used to allocate the
memory to the objects at the run time

• Memory is allocated at run time with the help of


'new' operator

• By using this constructor, we can dynamically


initialize the objects
3. Copy Constructor
• A copy constructor is a member function which
initializes an object using another object of the same
class

• It is of the form classname (classname &) and used


for the initialization of an object form another object
of same type
Syntax
4. Default Constructor
• Default constructor is the constructor which doesn’t
take any argument

• It has no parameters
Parameterized Constructor(with arguments)

• Constructor with arguments

• Used to initialize the various data elements of the


different object with different values when they are
created

• This can be done in two ways


1. By calling the constructor implicitly(shorthand method)
2. By calling the constructor explicitly
Parameterized Constructor(without arguments)

• Constructor with arguments having default values

• Used when most of the objects to be created are


likely to have the same value for some data
members

• Example : age of students will almost be same as 18


Difference between Default and parameterized
constructor with default arguments
Objects as arguments to function

• Objects are passed to functions using a standard call-by-


value mechanism

• A copy of an object is made when it is passed to a function

• When we want to initialize all data members of an object


with another object, we can pass objects and assign the
values of supplied object to the current object.

• For complex or large projects, we need to use objects as an


argument or parameter
Procedure to pass Object to function
Returning object from function
• In C++ we can pass class's objects as arguments and
also return them from a function the same way we
pass and return other variables

• No special keyword or header file is required to do


so
Procedure to return an object from function
Dynamic Initialization of Objects

• The class objects can be initialized at run time


(dynamically).

• We have the flexibility of providing initial values at


execution time.
Constructors and Primitive Types
• InC++, like derived type, i.e. class, primitive types
(fundamental types) also have their constructors

• Default constructor is used when no values are given


but when we given initial values, the initialization
take place for newly created instance.
• Example:

float x,y;
int a(10), b(20);
float i(2.5), j(7.8);
Destructor
• The syntax for declaring a destructor is :
-name_of_the_class()
{
}
• It does not take any parameter nor does it return any
value. Overloading a destructor is not possible and
can be explicitly invoked
• In other words, a class can have only one destructor.
A destructor can be defined outside the class
Characteristics of Destructors
• These are called automatically when the objects are
destroyed.

• Destructor functions follow the usual access rules


as other member functions.

• These de-initialize each object before the object


goes out of scope.

• No argument and return type (even void) permitted


with destructors.
Characteristics of Destructors
• These cannot be inherited.

• Static destructors are not allowed.

• Address of a destructor cannot be taken.

• A destructor can call member functions of it class

• An object of a class having a destructor cannot be a


member of a union.
Passing an Object as argument
• To pass an object as an argument we write the
object name as the argument while calling the
function the same way we do it for other variables

• Syntax:
function_name(object_name);

You might also like