Thursday 4 September 2014

CONSTRUCTOR AND DESTRUCTOR

CONSTRUCTOR

 
In C++ it is not allowed to initialize a variable directly in the class so what.
 
 we do, We use the concept of constructor to initialize the variables.
 
CONSTRUCTOR PROPERTIES
  1. Constructor name is same of its class.
  2. It has no return type.
  3. It is always declared in the public section of the class.
  4. As the first object of the class created ,Constructor get invoked.
  5. Constructor can be overloaded. 
Constructor is of three types
  • Constructor with no arguments
  • Constructor with arguments(Parameterized Constructor)
  • Copy constructor
Copy Constructor
If we want to use any value again and again then instead of passing that value again and again we pass the object which contains that particular value.
 
DESTRUCTOR
 
Destructor are quite similar to constructor but their main objectives is to destroy the object. Destructor properties are same but their declared with the help of (~) tilde sign.Their main objectives is to take memory back from the objects which were created during the design of programs.
 
 
 
Now We take a example in which we will use all of three constructors and these are Constructor with no arguments , Constructor with arguments Copy constructor and also Destructor. Lets see
 
#include<iostream.h>
#include<conio.h>
class test
{
int a;
public:
 
test()                                           //Constructor with no arguments
{
a=100;
}
 
test(int x)                                  //Constructor with arguments
{
a=x;
}
test(test &t)                              //Copy constructor
{
a=t.a;
}
void show()
{
cout<<a;
}
~test()                                      //Destructor
{
cout<<"object destroyed";
}
};
void main()
{
test t;
t.show();
test t1(100);
t1.show();
test t2(t1);
t2.show();
getch();
}
 
 
 
 


FUNCTIONS

FUNCTIONS

Functions are the self-defining blocks of code and they are used for repeated task or code.Every function on its execution return a value. Functions play an important role in c++ program development. Functions to give structured programming. Another advantage of using functions is that
it is reduced the size of a program by calling and using them at different places in the program.
 
 
How to declare a function
 
returntype functionname (no. of arguments or parameters)
{
 
     statement;
 
}
 
Syntax of Function
 
void show();                       // Function prototype
 
void main()
{
      ........
 
     show();                         // calling
       ........
 
}
 
void show()                       //Function definition
{
    ...............
    ...............
    ...............
 
}
 
INLINE FUNCTION
A Function declared with the help of inline keyword is known as Inline Function. Whenever a function is called lot of time wasted get wasted in the passing of the code to the function definition. so to reduce this problem with declared a function inline. By which the function call automatically get replaced by the definition this whole concept is based on inline declaration.
 
 
It works perfectly on simple function function but it has two disadvantages.
  1. It never works on the function which contains loops.
  2. It might not work on those function which has return type. 

FRIEND FUNCTION
 
A function is declared with the help of keyword friend is known as friend function .It acts as a bridge between two classes. If two classes wants to use same function it must be friend of both. Friend function can also access private data of the class.
 
Features of Friend function
  1. Friend function cannot be declared virtual.
  2. It is also defined outside the class.
  3. It takes class objects as arguments.
  4. It is never inside the class.
  5. We don't object to call this function.
VIRTUAL FUNCTION
A Function declared with the help of keyword virtual is termed as virtual function. It can be accessed only by the object of the class in which it has been declared.
 
PURE VIRTUAL FUNCTION
When a Virtual function is assigned 0 it becomes pure virtual function and the class which contains Pure Virtual function is termed as Abstract class. We cannot create object of the abstract class and abstract class always acts as the base class.

Wednesday 3 September 2014

DYNAMIC BINDING (LATE BINDING)

DYNAMIC BINDING

 
We study about the Dynamic Binding  and Binding refers to the linking of a procedure call to the code to be executed in response to the call. It is also known as Late Binding.When the variable are assigned value at  the run-time then it is known as Late Binding. Now lets see a example of Dynamic Binding
 
 
#include<iostream.h>
#include<conio.h>
 
class test
{
int a, b, c;
 
public:
void get(int i , int j, int k)
{
a=i;
b=j;
c=k;
}
 
void show()
{
cout<<a<<b<<c;
}
};
 
void main()
{
int  i, j, k;
test t;
cout<<"Enter the value of numbers";
cin>>i>>j>>k;
t.get();
t.show();
getch();
}
 
This example We have a class test and in which we have two functions get()and show() and we have three  variable a, b, c and we are assigned the value at the run-time.
 

EARLY BINDING

When variable are assigned value at the design this is known as Early Binding.lets take a example of
Early Binding.
 
 
 
#include<iostream.h>
#include<conio.h>
 
class test
{
int a, b, c;
 
public:
void get()
{
a=100;
b=300;
c=400;
}
 
void show()
{
cout<<a<<b<<c;
}
};
 
void main()
{
test t;
t.show();
getch();
}
 
 
 

 

 

POLYMORPHISM

POLYMORPHISM

Poly means many and morphism means forms . When yoy are performing multiple tasks with one name then the whole concept is known as polymorphism.
In Polymorphism we study two concept.
  1. Function overloading
  2. Function overriding

Function Overloading

In Function Overloading, We have a class in which functions are defined with same name but different arguments but their signature is different .Signature means return type function. All the Functions are called according to the no. of arguments they contain .This whole concept is known as Function Overloading.It is also called Design Time Polymorphism. Lets take a example.
 
#include<iostream.h>
#include<conio.h>
 
class ABC
{
 
public:
 
int add(int a, int b)
{
return(a*b);
}
 
int add(int a, int b, int c)
{
return(a*b*c);
}
 
int add(int a, int b, int c, int d)
{
return(a*b*c*d);
}
 
};
 
void main()
{
ABC t;
int k;
k=t.add(30,40);
cout<<k;
 
k=t.add(40,50,90);
cout<<k;
 
k=t.add(20,50,70,80);
cout<<k;
 
getch();
}
 

Function Overriding

It is the concept which is also uses the concept of Virtual Function. Suppose there are two classes A and B and B is the derived class of A in both classes. We have the function with same name but because the class function is declared virtual and whenever we create the object of derived class and calls the function the control first goes to the base class then it comes back to the derived class it is because the base class function is declared virtual and it can be called only by the object of the class in which it has been declared so, Derived class function will be executed. This whole procedure is termed Function Overriding.It is also called as Run time Polymorphism. Lets see a example of it
 
 
#include<iostream.h>
#include<conio.h>
 
class A
{
int a, b, c;
 
public:
 
virtual void get()                               // Virtual Function declared
{
cout<<"Enter the numbers";
cin>>a>>b;
c=a*a + b*b;
cout<<c;
}
};
 
class B : class A
{
int  i, j, k;
{
 
public:
 
void get()
{
cout<<"Enter the numbers";
cin>>i.>>j;
k=i*i + j*j + i*i +j*j;
cout<<k;
}
 
};
 
void main()
{
result r;
r.get();
info f;
f.get();
getch();
}
 
 

Tuesday 2 September 2014

INHERITANCE

INHERITANCE

Reusability is yet another important fearture of oop. Fortunatelty,C++ strongly supports the concept of reusability.The C++ class es can be reused in reused in several ways.

The mechanism of deriving a new class from old one is called inheritance.The OLD CLASS is referred to as the BASE CLASS and The NEW CLASS is DERIVED CLASS.

FORMS OF INHERITANCE
 
  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance
Lets take a example of Single Inheritance and we understand the concept of Inheritance.

Class XYZ

{
int a,b,c;

public:

void get()
{
cout<<"Enter the values"

cin>>a>>b;
c=a*b;
cout<<c;
}

};

class ABC : public XYZ            // symbol of Inheritance
{
int x,y,z;

public:

void show()
{
cout<<"Enter the values"
cin>>x>>y;
c=x*y;
cout<<z;
}

};

void main()
{
ABC t;
t.get();
t.show();
getch();
}

I hope that you understand concept of Single Inheritance .Lets take another example of Inheritance and Now we study concept of Multilevel Inhertance.

Class A

{
int a,b,c;

public:

void add()
{
cout<<"Enter the values"

cin>>a>>b;
c=a+b;
cout<<c;
}

};

class B : public A           //Inheritance
{
int x,y;

public:

void mul()
{
cout<<"Enter the values"
cin>>x;
y=c*x;
cout<<y;
}

};

class C: public B           //Inheritance
{
int p,q;

public:

void div()
{
cout<<"Enter the values"
cin>>p;
q=y/p;
cout<<q;
}

};

void main()
{
C t;
t.add();
t.mul();
t.div();
getch();
}
 

C++ PROGRAM STRUCTURE

C++ Program Structure

Header Files

Class_declaration

{

Variable

+

Methods(Functions)

};

void main()

{

classname objectname;           // object created

object.functionname();           // function call

getch();                                   // for display

}



Now we study How to make program in C++,we take a example and In this example we have a class and this class name is class A. In this program we add two variables a and b and their result will store in variable c.Lets see how to make program.

class A

{

int a,b,c;

void  ADD()

{
cout<<"Enter the numbers";
cin>>a>>b;
c=a+b;
cout<<c;
}

};

void main()

{
A t;
t.ADD();
getch();
}





 

Object Oriented Programming Structure


OOPS


C++ is  considered as more secure and efficient language because it works on the concept of accessing the information which is important at that time and ignoring the rest of the information.
C++ has various common features as compared to C and it has some advance features which are very important for the better used of any technology.Now come on the Basic Concept of OOPS.

Basic Concepts of Object Oriented Programming
1.Objects
2.Classes
3.Data Abstraction
4.Data Encapsulation
5.Inheritance
6.Polymorphism
7.Dynamic Binding
8.Message Passing

OBJECTS
 
An object is the real time entity with the help of which data of the class get accessed.
we create object by writing
classname  objectname;


CLASS

Class contains information of similar type or a class contains variables and methods.A class can be declared with the help of keyword class and class always get termninated by(;).
skeletion of the class

Class ClassName
{


variables
+
methods


};


Data Abstraction
It means we need to focus only on the essential part of the information and ignore the rest of the information.

i.e. Suppose we have 5 functions in a class but we have to use only three functions of this class so, we will use concept of Data Abstraction.


Data Encapsulation

The wrapping up of data and functions into a single unit is known as encapsulation.It  gives the concept of data hiding.
We have three access specifiers.
  1. public-It can be accessed outside the class.
  2. private-It cannot be accesses outside the class.
  3. protected-It can be accessed outside the class but only in derived class.

Inheritance

It gives the concept of reusability of the code defined inside the class.By mean we can add additional features to an existing class without modifying it.
  1. Multilevel
  2. Mulltiple
  3. Multipath
  4. Hybrid
  After we will more discuss on these points.
 
Polymorphism
 
Poly means many morphism means forms.When you are performing multiple tasks with one name
then the whole concept is known as polymorphism.
In polymorphism we study two concept-
  1. Function Overloading
  2. Function Overriding
 
Dynamic Binding
 
Binding refers to a linking of a procedure call to the code to be executed in response to the call.Dynamic Binding also knows as late binding means that the code associated with a given procedure call is not known until the time of call at run-time.It is associated with polymorphism
and inheritance.
 
Message Passing
 
An object-oriented program consists of a set of objects that communicate with each other.The
process of programming in an object-oriented language,therefore ,involves the following basic
steps:
  1. Creating classes that defines objects and their behaviour .
  2. Creating objects from class definitions
  3. Establishing communication among objects.