You are on page 1of 8

-----------------C++----------

1. C++ programs can use existing C software libraies


2. C folows Procedure Oriented Programming but C++ follows object oriented
programming
3.C++ is superset of C language

Object Oriented Programming

*Object(defination)
Features of object Oriented

*Object-is an instance of a class


*Class
*Encapsulation
*Abstraction
*data hiding
*Polymorphism
*Inheritance

Class
0.Class is a description of an Object.
1.Class is a blueprint of an Object.
2.It is user defined data type used to declare the objects.
3.Class is a description of Object's property set and set of operations.
4.Creating class is as good as defining a new data type
5.Class is a means to achive encapsulation
6.Object is a run time entity

class box
{
int l,b,h;
void setdimension(int x,int y,int z){-----}
}

Identifiers
*Indentifiers can be function names,variables,objects,Macros etc.
*Variables are the names of memory locations where we store data.

#Data-types
*int
*char
*float
*double
*void

*cout/cin is predefined object not function

<< is called insertion or put to operator


>> is called extraction or get from operator

*endl is manipulator (to move in new line)

int s=5*5; // this is dynamic initializer .here int is created in run time
Function

*Function is block of code performing a unit task


*It has name,return type,arguments
*It are predefined and user-defined

------example------

#include<iostream>
void main()
{
void fun(); //function decleration
cout<<"You are in main"<<endl;
fun(); //function call
}

void fun()
{
cout<<"You are in function"<<endl; //function definition
}

Formal and actual arguments

#include<iostream>
int sum(int,int);
void main()
{
int a=5,b=6;
int s=sum(a,b);//a,b are actual arguments
cout<<"sum is "<<s;
}

int sum(int x,int y)//x,y are formal arguments


{
return(x+y);
}

Call by value

#include<iostream>
int sum(int,int);
void main()
{
int a=5,b=6;
int s=sum(a,b);
cout<<"sum is "<<s;
}

int sum(int x,int y)


{
return(x+y);
}
Call by address

#include<iostream>
int sum(int *,int *);
void main()
{
int a=5,b=6;
int s=sum(&a,&b);
cout<<"sum is "<<s;
}

int sum(int *x,int *y)


{
return(*x+*y);
}

----------Call by reference------

#include<iostream>
int sum(int &,int &);
void main()
{
int a=5,b=6;
int s=sum(a,b);
cout<<"sum is "<<s;
}

int sum(int &x,int &y)


{
return(x+y);
}

Inline function
The type of function whose code is copied
in the called location is called inline function.
It save the execution time.

#include<iostream>
inline void fun();//function declaration
void main(){
cout<<"You are in main";
fun(); //function call
}

void fun()//function definition


{
cout<<"you are in function";
}

------------Default arguments----------
#include<iostream>
#include<conio.h>
using namespace std;
int add(int,int,int=0);
int main()
{
int a,b,c;
cout<<"Enter 2 numbers"<<endl;
cin>>a>>b;
cout<<"sum is "<<add(a,b);//only two arument use but in declaration int=0 for
third argument is given i.e default argument
cout<<"Enter 3 numbers"<<endl;
cin>>a>>b>>c;
cout<<"sum is "<<add(a,b,c)<<endl;
getch();
}

int add(int x,int y,int z){


return(x+y+z);
}

---------Funtion Overloading----------

*It is categorized in 3 way


a)Type of arguments
b)Number of arguments
c)Type and number of arguments

#include<iostream>
#include<conio.h>
using namespace std;
int area(int,int);
float area(int);
int main()
{
int r;
cout<<"Enter radius of circle"<<endl;
cin>>r;
float m=area(r);
cout<<"area of circle is "<<m<<endl;
int l,b,a;
cout<<"Enter rectangle length and bradth"<<endl;
cin>>l>>b;
a=area(l,b);
cout<<"Area of rectangle is "<<a<<endl;
getch();
return 0;
}

float area(int x){


return(3.14*x*x);
}

int area(int a,int b)


{
return (a*b);
}

---------Structure------------
*Structure is user defined data type which contain a collection of
different type of data under the same name.
*Structure is collection of dissimilar elements

------Class--------
/* Simple example*/
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Complex{
private:
int a,b;
public:
void get(int x,int y){
a=x;
b=y;
}
void show(){
cout<<b<<endl<<a;
}
};

int main()
{
Complex c1;//c1 is object
c1.get(3,4);
c1.show();
getch();
return 0;
}

/* Example*/
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Complex{
int a,b;
public:
void set_data(int x,int y)
{
a=x;
b=y;
}
void show_data(){
cout<<"a= "<<a<<endl<<"b= "<<b;
}
Complex add(Complex c){
Complex temp;
temp.a=a+c.a;
temp.b=b+c.b;
return(temp);
}

};
int main()
{
Complex c1,c2,c3;
c1.set_data(3,4);
c2.set_data(5,6);
c3=c1.add(c2);
c3.show_data();
getch();
return 0;
}

------------Static-----------------
1.Static local variable
2.Static member variable
3.Static member functions

----Static local variable----


/*void fun(){
static int x;/*memory allow at run time not from begining
*/default value 0 //lifetime throught the program
int y; //garbage value and allow memory after initialize
}
*/
----Static member variable----
*Declared inside class

class account{
private:
int balance; //Instance Member variable
static float roi; //Static member variable//class variable
public:
void setbalace(int b)
{
balance=b;
}
};

float account::roi=3.5f;//must declared outside class

int main()
{
account a1,a2;//Here,balance variable will be different for all object but roi will
be same for all and create only once
}
----Static member functions----
1.It is invoked with or without object

class account{
private:
int balance; //Instance Member variable
static float roi; //Static member variable//class variable
public:
void setbalace(int b)
{
balance=b;
}
static void set_roi(float r)//Static member function
{
roi=r;
}
};

float account::roi=3.5f;//must declared outside class

int main()
{
account a1,a2;
a1.set_roi(4.5f);
}

-----------Constructor----------
1.It is member function of a class.

-----------Operator Overloading----------
1.When an operator is overloaded with multiple jobs is operator overloading

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class complex{
int a,b;
public:
void setdata(int x,int y){
a=x;b=y;
}

void showdata(){
cout<<"a= "<<a<<endl<<"b= "<<b;
}
complex operator +(complex c) //before operator overloading WE USE-- complex
add(complex c)
{
complex temp;
temp.a=a+c.a;
temp.b=b+c.b;
return(temp);
}

};

int main()
{
complex c1,c2,c3;
c1.setdata(3,4);
c2.setdata(5,6);
c3=c1 + c2; //before operator overloading WE USE-- c3=c1.add(c2);
c3.showdata();
}

-----------Unary Operator---------
#include<iostream>
#include<conio.h>
using namespace std;
class complex
{
private:
int a,b;
public:
void setdata(int x,int y){
a=x;
b=y;
}
void showdata()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
complex operator -()
{
complex temp;
temp.a=-a;
temp.b=-b;
return(temp);
}
};
int main(){
complex c1,c2;
c1.setdata(3,4);
c2=-c1; //c1 calls - operator and no argument pass AND whatever - operator
return assign to c2
c2.showdata();
getch();
}

You might also like