C++ example programs
Oops concept and some example
#include<iostream>
using namespace std;
class num
{
int a,b;
public:
void set(int y,int x)
{
a=y;b=x;
}
void add()
{
cout<<a+b<<endl;
}
};
main()
{
num obj;
obj.set(2,4);
obj.add();
}
NOTE:The above saying that creating absort and making encapulation and accessing throught single interface that is object.
#include<iostream>
using namespace std;
namespace ownname
{
int a=10;
int b=12;
};
main()
{
cout<<ownname::a<<endl;
}
#include<iostream>
#include<math.h>
using namespace std;
float absolut(float x)
{
return x;
}
float absolut(double x,float y,float z)
{
cout<<"ceil value :"<<x<<" "<<ceil(x)<<endl;
cout<<"floor value :"<<y<<" "<<floor(y)<<endl;
cout<<"abs value :"<<z<<" "<<fabs(z)<<endl;
return ceil(x)+floor(y)+fabs(z);
}
main()
{
cout<<"sample value "<<absolut(1.234)<<endl;
cout<<"it sending absolut values "<<absolut(1.75,1.97,-1.28);
} #include<iostream>
using namespace std;
class math
{
public:
int multi(int x,int y)
{
return x*y;
}
double multi(int x,double y)
{
return x+y;
}
};
main()
{
math ob;
cout<<ob.multi(2,5)<<" "<<ob.multi(2,5.12);
} NOTE: The above saying about function overloading where your passing different type arrangements that are called function overloading.
#include<iostream>
using namespace std;
void myswap(int &a,int &b)
{
a=a+b;
b=a-b;
a=a-b;
}
main()
{
int a=10;
int b=12;
cout<<a<<" "<<b<<endl;
myswap(a,b);
cout<<a<<" "<<b<<endl; } NOTE: The above program saying that swap function in c++ example you notice one thing that you not putting int or void at main() because by default main will be int.
#include<iostream>
using namespace std;
class num
{
int a,b;
public:
void set(int y,int x);
void add()
{
cout<<a+b<<endl;
}
};
void num::set(int y,int x)
{
a=y;b=x;
}
main()
{
num obj;
obj.set(2,4);
obj.add();
} NOTE: The above program making defination outside of class using scope oprator.
#include<iostream>
using namespace std;
class test
{
int data;
float data1;
public:
void fun()
{
data=13;
cout<<data;
}
float fun1()
{
data1=13.1245;
return data1;
}
};
main()
{
test t;
t.fun();
cout<<"\n"<<(int)(t.fun1());
} NOTE: The above pro
#include<iostream>using namespace std;
class test
{
int data;
float data1;
public:
void fun()
{
data=13;
cout<<data;
}
float fun1()
{
data1=13.1245;
return data1;
}
};
main()
{
test t;
t.fun();
cout<<"\n"<<(int)(t.fun1());
} NOTE: The above pro
using namespace std;
class test
{
int x,y;
public:
void set(int b,int a)
{
x=a;
y=b;
}
float add()
{
return x+y;
}
};
main()
{
test t,t1;
t.set(2,8);
cout<<sizeof(t)<<endl;
cout<<"\n"<<(int)(t.add());
t1.set(5,9);
cout<<sizeof(t1)<<endl;
cout<<"\n"<<t1.add();
} #include<iostream>
using namespace std;
class student
{
int total;
char sec;
public:
int rollno;
int c;
char name[12];
float per;
void enter();
void temp();
void print()
{
cout<<"PERTA"<<per<<"\n"<<"names"<<name<<"\n"<<rollno<<endl;
cout<<"total"<<c<<"\n"<<"sec"<<sec;
}
};
void student::enter()
{
cin>>per>>name>>rollno;
c++;
cin>>sec;
}
void student::temp()
{
cout<<"enter fllows"<<"\n"<<"pertance"<<"\n"<<"name"<<"\n"<<"rollno"<<"\n"<<"total"<<"section"<<endl;
}
main()
{
int v=0;
student obj;
obj.temp();
do{
obj.enter();
cout<<"enter do want contiue"<<endl;
cin>>v;
}
while(v==1);
obj.print();
} #include<iostream>
using namespace std;
class test
{
int a,b;
public:
test()
{
cout<<"constor"<<endl;
a=20;
b=23;
}
};
main()
{
test obj;
} #include<iostream>
#include<cstring>
using namespace std;
class test
{
char name[12];
float sal;
public:
test()
{
cout<<"constor"<<endl;
// *name="remo";
strcpy(name,"remo");
sal=2.3;
}
public:
void display()
{
cout<<name<<"\n"<<sal<<endl;
}
};
main()
{
test obj;
obj.display();
} #include<iostream>
using namespace std;
class array
{
int arry[3];
public:
array(int a=0,int b=0,int c=0)
{
arry[0]=a;
arry[1]=b;
arry[2]=c;
}
void disp()
{
for(int i=0;i<3;i++)
cout<<arry[i]<<endl;
}
};
main()
{
array arrry(1,2,3);
arrry.disp();
} #include<iostream>
using namespace std;
class array
{
int *arry=new int[3] ;
public:
array(int a,int b,int c)
{
arry[0]=a;
arry[1]=b;
arry[2]=c;
}
void disp()
{
for(int i=0;i<3;i++)
cout<<arry[i]<<endl;
}
};
main()
{
array arrry(1,2,3);
arrry.disp();
} #include<iostream>
#include<math.h>
using namespace std;
class cylinder
{
int r,h;
public:
void set(int x,int y)
{
r=x;
h=y;
}
int area();
int volume()
{
int result=2*3.14*pow(r,2)*h;
return result;
}
};
int cylinder::area()
{
int res=(2*3.14*r*(r+h));
return res;
}
main()
{
cylinder obj;
obj.set(6,8);
int a=obj.volume();
int b=obj.area();
cout<<"volume "<<a<<" area "<<b<<endl;
} #include<iostream>
#include<cstring>
using namespace std;
class fnd
{
int size;
char *s;
public:
fnd(char *str=NULL);
~fnd()
{
delete[] s;
}
fnd(fnd &);
void print()
{
cout<<s<<endl;
}
void change(char *);
};
fnd::fnd(char *str)
{
size=strlen(str);
s=new char(size+1);
strcpy(s,str);
}
void fnd::change(char *str)
{
delete [] s;
size=strlen(str);
s=new char(size+1);
strcpy(s,str);
}
fnd ::fnd (fnd &t)
{
size=t.size;
s=new char[size+1];
strcpy(s,t.s);
}
main()
{
fnd str1("cranes");
fnd str2=str1;
str1.print();
str2.print();
str2.change("cranesvaristy");
str1.print();
str2.print();
} #include<iostream>
#include<cstring>
using namespace std;
class test
{
int a;
float f;
char name[12];
public:
test(int x,float d,char *s)
{
a=x;
f=d;
strcpy(name,s);
}
test(test &m)
{
a=m.a;
f=m.f;
name=m.s;
}
void display()
{
cout<<a<<f<<name<<endl;
}
};
main()
{
test t1(12,1.2,"remo");
test t2(t1);
//test t1(test t2(234,21.5,"raja")); redeclration error
t1.display();
//t2.x=12;
} #include<stdio.h>
int a;
int& fun()
{
return a;
}
main()
{
fun();
// cout<<fun(1,2)<<endl;
// cout<<fun(1,2,4,9)<<endl;
// cout<<fun(1,2,6)<<endl;
}
#include<iostream>
using namespace std;
int fun(int a=0,int=0,int c=1,int d=2)
{
return a+b+c+d;
}
main()
{
cout<<fun(1,2)<<endl;
cout<<fun(1,2,4,9)<<endl;
cout<<fun(1,2,6)<<endl;
} #include<iostream>
using namespace std;
class point
{
public:
point()
{
cout<<"narmal"<<endl;
}
/* point (point &)
{
cout<<"copy"<<endl;
}*/
};
main()
{
point *t1;
point *t2;
t1=new point();
t2=new point(*t1);
point t3=*t1;
point t4;//it call only once constructor
// t4=t3;//dummy
//point t4=t3;//redeclare error
} #include<iostream>
using namespace std;
main()
{
int *p=new int[5];
for(int i=0;i<5;i++)
cin>>p[i];
for(int i=0;i<5;i++)
cout<<p[i]<<endl;
delete p;
} #include<iostream>
using namespace std;
class ope
{
int a;
public:
ope(int x=0)
{
a=x;
}
void show()
{
cout<<a<<endl;
}
void operator ++(int);
};
void ope::operator ++(int v)
{
a++;
}
main()
{
ope o1(10);
o1.show();
o1++;
o1.show();
}
#include<iostream>
using namespace std;
class test
{
int a,b;
public:
test (int a,int b)
{
this->a=a;
this->b=b;
}
friend void display(test m);
};
void display(test m)
{
cout<<m.a<<" "<<m.b<<endl;
}
main()
{
test t(1,3);
display(t);
} #include<iostream>
using namespace std;
class test
{
int k,l;
public:
test (int a=0,int b=0)
{
k=a;
l=b;
}
friend void display();
};
void display()
{
test m;
cout<<m.l<<" "<<m.k<<endl;
}
main()
{
// test t(1,3);
display();
} #include<iostream>
using namespace std;
class test
{
int k,l;
public:
test (int a=10,int b=10)
{
k=a;
l=b;
}
friend void display();
};
void display()
{
test m;
cout<<m.l*m.k<<endl;
}
main()
{
display();
} #include<iostream>
using namespace std;
class ope
{
int a;
public:
ope(int x=0)
{
a=x;
}
void show()
{
cout<<a<<endl;
}
void operator ++();
};
void ope::operator ++()
{
++a;
}
main()
{
ope o1(10);
o1.show();
++o1;
o1.show();
} #include<iostream>
using namespace std;
class A
{
int a;
public:
A()
{
a=12;
}
friend class B;
};
class B
{
int a;
public:
void show(A x)
{
x.a=12`;
cout<<x.a=67;
}
};
main()
{
A a;
B b;
b.show(a);
} #include<iostream>
using namespace std;
int fun(int a,int b)
{
return a+b;
}
char* fun(char *ch)
{
return ch;
}
main()
{
cout<<fun(12,13)<<fun("Abc")<<endl;
}
#include<iostream>
using namespace std;
class cal
{
int a,b;
public:
inline int set(int x,int y);
inline int add();
inline int sub();
inline int multi();
#define squre a*b
};
inline int cal::set(int x,int y)
{
a=x;
b=y;
}
inline int cal::add()
{
return a+b;
}
inline int cal::sub()
{
return a-b;
}
inline int cal::multi()
{
return a*b;
}
main()
{
int a,b,c;
cal obj;
cout<<squre;
obj.set(12,3);
a=obj.add();
b=obj.sub();
c=obj.multi();
cout<<"add"<<a<<"\n"<<"\nsub"<<b<<"\nmulti"<<c;
} #include<iostream>
using namespace std;
inline int fun(int &a,int &b)
{
a=a+b;
b=a-b;
a=a-b;
}
main()
{
int a=12,b=13;
fun(a,b);
cout<<a<<b;
} #include<iostream>
using namespace std;
namespace frist
{
int a=10;
void fun()
{
//cout<<secound::a;
cout<<"this is frist"<<endl;
}
}
namespace secound
{
float a=10.12;
void fun()
{
cout<<"this is secound"<<endl;
}
}
main()
{
cout<<frist::a<<endl;
cout<<secound::a<<endl;
frist::fun();
secound::fun();
}
#include<iostream>
using namespace std;
namespace frist
{
int a=10;
void fun()
{
cout<<"this is frist"<<endl;
}
}
namespace secound
{
float a=10.12;
void fun()
{
cout<<"this is secound"<<endl;
}
}
//using namespace frist;
main()
{
{
using namespace frist;
cout<<a<<endl;
// cout<<a<<endl;
fun();
}
{
using namespace secound;
cout<<a<<endl;
}
} #include<iostream>
#include<stdlib.h>
using namespace std;
main()
{
char *p=new char;
cout<<*p<<endl;
*p='z';
cout<<*p<<endl;
delete p;
int *pt=(int*)malloc(sizeof(int));
*pt=24;
cout<<*pt<<endl;//delete memory
}
Comments
Post a Comment