Object Oriented Concept Class & Object
Object Oriented Concept Class & Object
Class is user defined data types ,& it contains its own collection of data
member & member function .
Syntax
Class class_name
Class Add
Cin>>a >>b;
};
Object
Syntax
Class_name object_name ;
Example
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;
CONSTRUCTOR
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
{
Body of constructor
}
Member function
} ; End of class
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);
}}
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
Output
Constructor called
Constructor called
Destructor called
Destructor called
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 }
function is called.
are not possible for normal function calls. Other optimizations can be
context.
because inline can yield less code than the function call preamble and
return.
variable resource utilization. This means that when inline function body
used for the variables will also get increased. So if after function inlining
2) If you use too many inline functions then the size of the lakshetty
code.
3) Too much inlining can also reduce your instruction cache hit rate,
thus reducing the speed of instruction fetch from that of cache memory
changes the code inside the inline function then all the calling location
code once again to reflect the changes, otherwise it will continue with old
functionality.
#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
Friend Functions
class Box {
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
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>
Namespace ns1
Int a=5;
} Adarsh
lakshetty
Namespace ns2
Char a[]=”hello”;
int main ()
Cout<<ns1::a<<endl;
Cout<<ns2::a<<endl;
Return 0;
}
Output : 5
hello
Adarsh
lakshetty