0% found this document useful (0 votes)
14 views12 pages

Lecture 5

This document discusses Object Oriented Programming in C++, focusing on classes and objects. It covers topics such as passing objects as function arguments, returning objects from functions, parameterized constructors, constructor overloading, and destructors. The document provides code examples to illustrate these concepts and emphasizes the importance of destructors in memory management.

Uploaded by

catsofthedayss
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)
14 views12 pages

Lecture 5

This document discusses Object Oriented Programming in C++, focusing on classes and objects. It covers topics such as passing objects as function arguments, returning objects from functions, parameterized constructors, constructor overloading, and destructors. The document provides code examples to illustrate these concepts and emphasizes the importance of destructors in memory management.

Uploaded by

catsofthedayss
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/ 12

Object Oriented

Programming

Lecture 5
Class & Object (Part 2)

1
Object as Function Argument:

Procedure to Pass
Object to Function

In C++ programming,
objects can be passed
to function in similar
way as variables and
structures.
Object asReturning
Function Argument:
Object from Function

Returning Object
from Function

The syntax and procedure


to return object is similar
to that of returning
structure from function.
Object as Function Argument:
class Complex void Display()
{ {
private:
int real; cout<<"Sum="<<real<<"+"<<imag<
int imag; <"i";
public: }
void Read() { };
cout<<"Enter real and imaginary number int main()
respectively:"<<endl; {
cin>>real>>imag; Complex c1,c2,c3;
} c1.Read();
void Add(Complex comp1,Complex comp2) c2.Read();
{ c3.Add(c1,c2);
real=comp1.real+comp2.real; c3.Display();
imag=comp1.imag+comp2.imag; return 0;
} }
Parameterized Constructor
C++ permits us to achieve the objects but passing
Definition argument to the constructor function when the object
are created. The constructor that can take arguments
are called parameterized constructors
class abc
{
int m,n;
public:
abc(int x,int y); //parameterized constructor
................
Example .................
};
abc::abc(int x,int y)
{
m=x;n=y;
}
Parameterized Constructor
#include<iostream>
using namespace std; void Display()
class Example {
{ cout<<"Values :"<<a<<"\t"<<b<<"\t"<<c;
int a,b,c; }
public: };
Example(int x,int y,int z)
{ int main()
a=x; {
b=y; Example Object(10,20,30);
c=z; Object.Display();
cout<<"This is Constructor\n"; return 0;
} }
Constructors Overloading
Constructors Overloading are used to increase the flexibility of a class
by having more number of constructor for a single class. By have more
than one way of initializing objects can be done using overloading
constructors.
Constructors Overloading
#include<iostream> void calculate1()
using namespace std; {
class cpy cout<< fact+ real;
{ }
int var,fact,real; };
public:
cpy(int temp) int main()
{ {
var=temp; int n,m;
} cout<<"Enter the Number : ";
cpy(){} cin>>n>>m;
cpy(int a, int b){ cpy obj;
fact=a; cpy obj1(n);
real=b; cpy obj2(n,m);
} obj.calculate();
void calculate() obj1.calculate();
{ obj2.calculate1();
cout<<var<<endl; return 0;
} }
Destructor
 It is used to destroy the object that has been created by a constructor.

 It is a member function whose name is the same as the class name but
is preceded by tilde (~) symbol.

 A class can’t have more than one destructor. It takes no arguments and
no return types can be specified for it (not even void).

 It is called automatically by the compiler when an object is destroyed.

The general syntax of the constructor and destructor is:-


Class Class_name
{
Private:
Public:
Class_name (); // constructor
~Class_name (); //destructor
};
Programming Example
Programming Example
#include <iostream> // Implement MyClass destructor.
using namespace std; MyClass::~MyClass()
{
class MyClass cout<< "Destructing ...\n";
{ }
public: main()
int x; {
MyClass(); // constructor
~MyClass(); // destructor MyClass ob1;
}; MyClass ob2;
// Implement MyClassconstructor. cout <<ob1.x<< " " <<ob2.x<<"\n";
MyClass::MyClass()
{ }
x = 10;
}
Need of destructor
Constructor allocates memory to the object. This allocated memory must be de-
allocated before the object is destroyed.

This job of memory de-allocation from object is done by special member


function of the class. This member function is destructor.

Hence, the most common use of destructor is to de-allocate and release


memory that was allocated for the object by the constructor.

You might also like