0% found this document useful (0 votes)
24 views

Object Oriented Concept Class & Object

The document discusses object oriented concepts including classes, objects, constructors, destructors, and inline functions in C++. It defines a class as a user-defined data type that contains data members and member functions. An object is an instance of a class. Constructors initialize objects when they are created and destructors destroy objects when they go out of scope. Inline functions replace function calls with the function body to reduce overhead.

Uploaded by

M.A raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Object Oriented Concept Class & Object

The document discusses object oriented concepts including classes, objects, constructors, destructors, and inline functions in C++. It defines a class as a user-defined data type that contains data members and member functions. An object is an instance of a class. Constructors initialize objects when they are created and destructors destroy objects when they go out of scope. Inline functions replace function calls with the function body to reduce overhead.

Uploaded by

M.A raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Object oriented concept

CLASS & OBJECT


Class definition :

Class is user defined data types ,& it contains its own collection of data
member & member function .

Syntax

Class class_name

{ Access specifier // private , public or protected

Data member // local variable declaration

Member function // function to access data member of class

}; // class is terminated by semi colon

Where class : is keyword

Access specifier : In general access specifiers are the access restriction


imposed during the derivation of different subclasses from the base class.

Public - The members declared as Public are accessible from


outside the Class through an object of the class.
Protected - The members declared as Protected are accessible from
outside the class BUT only in a class derived from it.
Private - These members are only accessible from within the class.
No outside Access is allowed.
Example

Class Add

{ int a,b,sum; // data member Adarsh


Public Void sum () // member function lakshetty

{ cout<< “ enter the value for a & b \n”

Cin>>a >>b;

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 1


Object oriented concept

};

Object

Object is an instance of class

Syntax

Class_name object_name ;

Example

In above class add object is created as

Add a; // object a is created

Access data & member function for class

Dot (.) operator is used to access the access & initialize data member &
member function of class

class Studentcse
{
public: int rollno;
string name;
}; Adarsh
lakshetty
Void main()
{
Studentcse A;
Studentcse B;

// initial values for data member of class studentcse using object A


A.rollno=1;
A.name="Adarsh";

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 2


Object oriented concept

// initial values for data member of class studentcse using object B


B.rollno=2;
B.name="vishal";
cout <<"Name and Roll no of object A is: "<< A.name << "-" <<A.rollno;
cout <<"Name and Roll no of object B is: "<< B.name << "-" << B.rollno;
}
Output

Name and Roll no of object A is: Adarsh


Name and Roll no of object B is: vishal

CONSTRUCTOR

A constructor is a special type of member function that initializes


an object automatically when it is created. Compiler identifies a given
member function is a constructor by its name and the return type.

Rules of Constructors
1. Constructor should be same name as the class and declared in the
public section of the class.
2. Constructors are invoked automatically whenever the object is
created.
3. Constructors do not have return type.
4. Constructor can not be inherited, but from the derived class we can
call the base class constructors.
5. Constructor can not be virtual.
6. It is not possible to refers to the address of constructors.
7. It is not possible to use the constructor as member of union if the object
is created with constructor.
8. constructor can be overloaded
9. Tt allocate memory for class

Syntax Adarsh
lakshetty
Class class_name
{ data member
Public: class_name( ) // constructor
{

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 3


Object oriented concept

Body of constructor
}

Member function
} ; End of class

Types of Constructors in C++


Constructors are of three types:

1. Default Constructor
2. Parametrized Constructor

Default Constructors
Default constructor is the constructor which doesn't take any argument.
It has no parameter.

Class cse
{
Public: cse( )
{
Cout<<” Default constructor is invoked”;
}
}

Parameterized Constructors
These are the constructors with parameter. Using this Constructor you
can provide different values to data members of different objects, by
passing the appropriate values as argument.

Class CSE
{
Adarsh
Public: CSE(int a)
{ lakshetty
Cout<<”square of number “<<a<<”is=”<<(a*a);
}}

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 4


Object oriented concept

Destructors
Destructor is a special class function which destroys the object as soon as
the scope of object ends. The destructor is called automatically by the
compiler when the object goes out of scope.

The syntax for destructor is same as that for the constructor, the class
name is used for the name of destructor; with a tilde ~ sign as prefix to
it.
class CSE
{
public: ~CSE() // defining destructor for class
{
// statement
}
};

class A
{
A() // constructor
{
cout << "Constructor one called";
}

~A() // destructor
{
cout << "Destructor one called";
}
};

int main()
{
A obj1; // Constructor Called
int x=1
if(x) Adarsh
{
lakshetty
A obj2; // Constructor Called
} // Destructor Called for obj2
} // Destructor called for obj

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 5


Object oriented concept

Output
Constructor called

Constructor called

Destructor called

Destructor called

Member function declaration outside the class

Class cube
{ int a;
Public: cube(int x)
{ a=x;
}
int cub( ); // member function declared
};
int cube :: cub( ) // member function outside the class
{ return (a*a*a);
}
Main( )
{ cube c(5);
Cout<<”cube of given number is “<<a<<”is :”<<c.cub;
}

Inline function

is a function which when invoked requests the compiler to replace the calling
statement with its body. A keyword inline is added before the function name
to make it inline. It is an optimization technique used by the compilers as it
saves time in switching between the functions otherwise. Member functions of a
class are inline by default even if the keyword inlineis not used.

Syntax
Adarsh
Inline return_type function_name (parameter list)
lakshetty
{ body of inline }

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 6


Object oriented concept

Inline functions provide following advantages:

1) Function call overhead doesn’t occur.

2) It also saves the overhead of push/pop variables on the stack when

function is called.

3) It also saves overhead of a return call from a function.

4) When you inline a function, you may enable compiler to perform

context specific optimization on the body of function. Such optimizations

are not possible for normal function calls. Other optimizations can be

obtained by considering the flows of calling context and the called

context.

5) Inline function may be useful (if it is small) for embedded systems

because inline can yield less code than the function call preamble and

return.

Inline function disadvantages:

1) The added variables from the inlined function consumes additional

registers, After in-lining function if variables number which are going to

use register increases than they may create overhead on register

variable resource utilization. This means that when inline function body

is substituted at the point of function call, total number of variables used

by the function also gets inserted. So the number of register going to be

used for the variables will also get increased. So if after function inlining

variable numbers increase drastically then it would surely cause an

overhead on register utilization. Adarsh

2) If you use too many inline functions then the size of the lakshetty

binary executable file will be large, because of the duplication of same

code.

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 7


Object oriented concept

3) Too much inlining can also reduce your instruction cache hit rate,

thus reducing the speed of instruction fetch from that of cache memory

to that of primary memory.

4) Inline function may increase compile time overhead if someone

changes the code inside the inline function then all the calling location

has to be recompiled because compiler would require to replace all the

code once again to reflect the changes, otherwise it will continue with old

functionality.

5) Inline functions may not be useful for many embedded systems.

Because in embedded systems code size is more important than speed.

6) Inline functions might cause thrashing because inlining might

increase size of the binary executable file. Thrashing in memory causes

performance of computer to degrade.

#include <iostream>
using namespace std;
inline int exp(int x, int y, int z)
{
return (x + y) * z;
}
int main()
{
cout << exp(4,5,7) << endl;
cout << exp(4,5,6) << endl;
cout << exp(4,7,5) << endl;
cout << exp(7,4,6) << endl;
return 0;
Adarsh
}
lakshetty
Output:

63 54 55 66

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 8


Object oriented concept

Friend Functions

The private member data of a class can be accessed only by member


functions of that class.

Declaration of friend function

class Box {
double width;

public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};

Example on friend function


#include <iostream.h>

class Box {
double width;

public:
friend void printWidth( Box box );
void setWidth( double wid );
};

// Member function definition


void Box::setWidth( double wid ) {
width = wid;
}

// Note: printWidth() is not a member function of any class.


void printWidth( Box box ) {
Adarsh
/* Because printWidth() is a friend of Box, it can
lakshetty
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 9


Object oriented concept

// Main function for the program


int main() {
Box box;

// set box width without member function


box.setWidth(10.0);

// Use friend function to print the wdith.


printWidth( box );

return 0;
}
Output

Width of box : 10

Namespace

Namespace are used to group the entities like class , variable , object
function under a name .namespace helps to divide global scope into sub
scopes where each sub scope has its own name

#include<iostream>

Using namespace std;

Namespace ns1

Int a=5;

} Adarsh
lakshetty

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 10


Object oriented concept

Namespace ns2

Char a[]=”hello”;

int main ()

Cout<<ns1::a<<endl;

Cout<<ns2::a<<endl;

Return 0;

}
Output : 5

hello

Adarsh
lakshetty

Asst Prof Adarsh Lakshetty BKEC , Dept of CSE Page 11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy