Practical no.
1- Data member and member functions
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
class student
{
char name[30];
int rollno, m1, m2, m3, total;
float avg;
public:
void getdata()
{
cout<<"Enter name:";
cin>>name;
cout<<"\n Enter Roll No:";
cin>>rollno;
cout<<"\n Enter Marks1:";
cin>>m1;
cout<<"\n Enter Marks2:";
cin>>m2;
cout<<"\n Enter Marks3:";
cin>>m3;
}
void calculate()
{
total=m1+m2+m3;
avg=total/3;
}
void display();
};
void student::display()
{
cout<<"\n Name="<<name<<endl;
cout<<"\n rollno="<<rollno<<endl;
cout<<"\n Marks1="<<m1<<endl;
cout<<"\n Marks2="<<m2<<endl;
cout<<"\n Marks3="<<m3<<endl;
cout<<"\n total="<<total<<endl;
cout<<"\n Average="<<avg<<endl;
}
int main()
{
student p;
p.getdata();
p.calculate();
p.display();
getch();
}
Practical no.2- Branching and looping statements using
classes
#include <iostream>
#include <conio.h>
using namespace std;
class control
{
public:
int no;
void readno( )
{
cout<<"\n Enter a number:";
cin>>no;
}
void callif( )
{
readno( );
if(no%2==0)
{
cout<<no<<"is even no"<<endl;
}
else
{
cout<<no<<"is odd no"<<endl;
}
}
void callwhile( )
{
readno( );
int rem,rev,sum;
while(no!=0)
{
rem=no%10;
rev=(rev*10)+rem;
no=no/10;
}
cout<<"\n Reverse of a number is "<<rev;
cout<<"\n Sum of digits"<<sum;
}
void callfor( )
{
readno( );
cout<<"\n Table of"<<no<<endl;
for(int i=1; i<=10; i++)
{
cout<<no*i<<endl;
}
}
};
int main( ) {
control e;
char ans;
int n;
do
{
cout<<"Enter your choice;\n";
cout<<"1.if condition:\n";
cout<<"2.while loop:\n";
cout<<"3.for loop\n";
cin>>n;
switch(n)
{
case1:
cout<<"\n Even odd";
c.callif( );
break;
case2:
cout<<"\n Reverse of number";
c.callwhile( );
break;
case3:
cout<<"\n Table generator";
c.callfor( );
break;
default:
cout<<"\n Invalid choice";
}
cout<<"\n Do you want to continue press y for yes:";
cin>>ans;
} while(ans=='y');
return 0
}
Practical no.3- Dimensional arrays
1D array:
#include <iostream>
#include <conio.h>
using namespace std;
class Array_1D
{
public:
int a[5], b[5], c[5];
void acceptArray(int size)
{
for(int i=0; i<size; i++)
{
cout<<"\n Enter array element["<<i<<"]"<<endl;
cin>>a[i];
}
}
void sort( )
{
int t;
for(int i=0; i<sizeof(a)/sizeof(a[0]);i++)
{
for(int j=i+1; j<sizeof(a)/sizeof(a[0]);j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
void show( )
{
cout<<"\n Sorted Array Elements are:"<<endl;
for(int i=0; i<sizeof(a)/sizeof(a[0]);i++)
{
cout<<a[i]<<"\t";
}
}
};
int main( ){
Array_1D o;
o.acceptArray(5);
o.sort( );
o.show( );
return 0;
}
2D array:
#include <iostream>
#include<conio.h>
using namespace std;
class Array_2D
{
public:
int b[5][5];
void acceptArray()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<"\n Enter array element["<<i<<"]["<<j<<"]"<<endl;
cin>>b[i][j];
}
}
}
Array_2D addition(Array_2D o1,Array_2D o2)
{
int t;
Array_2D o3;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
o3.b[i][j]=o1.b[i][j]+o2.b[i][j];
}
}
return o3;
}
void show()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<endl;
}
}
};
int main()
{
Array_2D o1,o2,o3;
cout<<"\n Array 1";
o1.acceptArray();
cout<<"\n Array? 2";
o2.acceptArray();
o3=o3.addition(o1,o2);
cout<<"\n Array 1"<<endl;
o1.show();
cout<<"\n Array 2"<<endl;
o2.show();
cout<<"\n Addition of Two arrays:"<<endl;
o3.show();
return 0;
}
Practical no.4- Scope resolution operator
#include <iostream>
#include<conio.h>
using namespace std;
int m=10;
int main()
{
cout<<"\n Use of Scope Resolution operator (::) for variables scope"<<endl;
int m=20;
{
int k=m;
int m=30;
cout<<"k="<<k<<endl;
cout<<"m="<<m<<endl;
cout<<"::m="<<::m<<endl;
}
cout<<"m="<<m<<endl;
cout<<"m="<<::m<<endl;
return 0;
Practical no.5- Constructor and destructor
1) Constructor:
#include <iostream>
using namespace std;
class construct
{
public:
int a,b;
// default constrctor
construct()
{
a=10;
b=20;
}
construct (int x,int y)
{
a=x;
b=y;
}
construct (construct & c)
{
a=c.a;
b=c.b;
}
void show()
{
cout <<"a:"<<a<<endl<<"b:"<<b;
}
};
int main()
{
cout<<"\n Default constructor called automatically when the object is created";
construct c;
cout <<"\n Parameterised constructor";
construct c1 (30,40);
c1.show();
cout <<"\n Copy constructor";
construct c2 (c);
c2.show();
return 1;
Destructor:
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1;
Employee e2;
return 0;
}
Practical no.6- Public, protect and private scope
#include <iostream>
#include<conio.h>
using namespace std;
class base
{
public:
int a;
void assign(int x,int y, int z)
{
a=x;
b=y;
c=z;
display();
}
private:
int b;
void display()
{
cout<<"\n Public:"<<endl<<"a="<<a<<endl;
cout<<"\n Private:"<<endl<<"b="<<b<<endl;
cout<<"\n Protected:"<<endl<<"c="<<c<<endl;
}
protected:
int c;
};
class derived:public base
{
public:
void show()
{
cout<<"\n public and protected members can be access in inherited class:
\n";
int m=a*c;
cout<<"Multiplication="<<m<<endl;
}
};
class demo
{
public:
void print()
{
base p;
cout<<"\n public members can be access in any class: \n";
p.a=10;
int d=p.a/2;
cout<<"Division="<<d<<endl;
}
};
int main()
{
derived d;
d.assign(10,20,30);
d.show();
demo o;
o.print();
base p;
p.a=25;
cout<<"\n public members can be access in anywhere in program"<<endl;
cout<<"Subtraction="<<p.a-10<<endl;
return 0;
}
Practical no.7- Single inheritance
#include <iostream>
#include<conio.h>
using namespace std;
class A
{
public:
int a;
void display(int x)
{
cout<<"base class method calling";
a=x;
cout<<"\n a="<<a<<endl;
}
};
class B:public A
{
public:
int b;
void show(int x, int y)
{
cout<<"derived class method calling";
b=x;
a=y;
cout<<"\n a="<<a<<endl;
cout<<"\n b="<<b<<endl;
}
};
int main(){
cout<<"\n single inheritance one base and one derived class"<<endl;
B d;
d.show(10,20);
d.display(30);
return 0;
}
Practical no.8
Multiple inheritance:
#include <iostream>
#include<conio.h>
using namespace std;
class mammal
{
public:
mammal()
{
cout<<"Mammals are a group of vertebrate animals"<<endl;
}
};
class wingedAnimal
{
public:
wingedAnimal()
{
cout<<"Winged animal can flap ."<<endl;
}
};
class Bat: public mammal, public wingedAnimal{};
int main()
{
cout<<"multiple inheritance \n";
Bat b1;
return 0;
Heirarchical inheritance:
>
#include<conio.h>
using namespace std;
class A
{
public:
int x,y;
void getdata()
{
cout<<"Enter value x and y: \n ";
cin>>x>>y;
}
};
class B :public A
{
public:
void product()
{
cout<<"\n product="<<x*y<<endl;
}
};
class C :public A
{
public:
void sum()
{
cout<<"\n sum="<<x-y;
}
};
int main()
{
cout<<"Heirachical inheritance";
B obj1;
C obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Practical no.9- Multilevel inheritance and derived class
#include <iostream>
#include<conio.h>
using namespace std;
class A
{
public:
int a;
A(int x)
{
cout<<"Base class constructor calling";
a=x;
cout<<"\n a="<<a<<endl;
}
};
class B:public A
{
public:
int b;
B(int x,int y):A(y)
{
cout<<"Intermediate derived class constructor calling";
b=x;
cout<<"\n b="<<b<<endl;
}
};
class C: public B
{
public:
int c;
C(int x,int y, int z):B(x,y)
{
cout<<"Derived Class Constructor Calling";
c+z;
cout<<"\n c="<<c<<endl;
}
};
int main()
{
cout<<"\n Multilevel Inheritance with Constructors"<<endl;
C o(10,20,30);
return 0;
Practical no.10
1) Friend function:
#include <iostream>
#include<conio.h>
using namespace std;
class two;
class one
{
int d1;
public:
void setValue(int x)
{
d1=x;
}
friend void sum(one , two);
void show()
{
cout<<"You entered distance1="<<d1<<endl;
}
};
class two
{
int d2;
public:
void setValue(int x)
{
d2=x;
}
friend void sum(one,two);
void show()
{
cout<<"You entered distance2="<<d2<<endl;
}
};
void sum(one o, two t)
{
int ans=o.d1+t.d2;
cout<<"Sum of two distances are :"<<ans;
}
int main()
{
//clscr();
one o;
o.setValue(120);
o.show();
two t;
t.setValue(150);
t.show();
sum(o,t);
return 0;
}
2) Inline function:
#include<iostream>
using namespace std;
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout<<"Enter first value:";
cin>>a;
cout<<"Enter second value:";
cin>>b;
}
inline void operation :: sum()
{
add = a+b;
cout<<"Addition of two numbers:"<<a+b<<"\n";
}
inline void operation :: difference()
{
sub = a-b;
cout<<"Difference of two numbers:"<<a-b<<"\n";
}
inline void operation :: product()
{
mul = a*b;
cout<<"Product of two numbers:"<<a*b<<"\n";
}
inline void operation :: division()
{
div = a/b;
cout<<"Division of two numbers:"<<a/b<<"\n";
}
int main()
{
cout<<"Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
3) This pointer:
#include<iostream>
using namespace std;
class test
{
private:
int x;
public:
void setX (int x)
{
this->x=x;
}
void print) {cout<<"x="<<x<<endl;}
};
int main()
{
cout<<"Use of this pointer"<<endl;
test obj;
int x=20;
obj.setX(x);
obj.print();
return 0;
}
Practical no.11- Overloading and Overriding
1) Overloading:
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);
int mul(int a, int b)
{
return a*b;
}
float mul(double x, int y)
{
return x*y;
};
int main()
{
int r1= mul(6,7);
float r2= mul(0.2,3);
cout<<"r1 is :"<<r1<<endl;
cout<<"r2 is :"<<r2<<endl;
return 0;
2) Overriding:
#include<iostream>
using namespace std;
class animal{
public:
void eat(){
cout<<"Eating...";
}
};
class Dog:public animal
{
public:
void eat(){
cout<<"Eating bread....";
}
};
int main (void)
{
Dog d=Dog();
d.eat();
return 0;
Practical no.12- Use of pointers
#include<iostream>
using namespace std;
class animal{
public:
void eat(){
cout<<"Eating...";
}
};
class Dog:public animal
{
public:
void eat(){
cout<<"Eating bread....";
}
};
int main (void)
{
Dog d=Dog();
d.eat();
return 0;
}
Practical no.13- Text and binary file handling
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
class student
{
public:
int roll;
char name[15],f_name[20];
void put();
void get();
void switch_case();
}; student s;
void student::put()
{
fstream file;
cout<<"Enter Sudents details \n";
cout<<"Enter roll no: ";
cin>>roll;
cout<<"\n Enter name: ";
cin>>name;
cout<<"\n Enter father name: ";
cin>>f_name;
file.open("stu.dat",ios::out|ios::app);
file.write((char *)this,sizeof(student));
file.close();
s.switch_case();
}
void student::get()
{
int temp;
cout<<"\n Enter roll no: ";
cin>>temp;
fstream file;
file.open("stu.dat",ios::in);
file.seekg(0,ios::beg);
while(file.read((char *)this,sizeof(student)));
{
if(roll=temp)
{
cout<<"\n roll no. "<<roll<<endl;
cout<<"\n stu name: "<<name<<endl;
cout<<"\n father name: "<<f_name<<endl;
}
}
file.close();
getch();
s.switch_case();
}
void student::switch_case()
{
int i;
cout<<"\n Enter your choice (1-Read, 2-Write, 3-exit): ";
cin>>i;
switch(i)
{
case 1:
s.get();
break;
case 2:
s.put();
break;
case 3:
exit(0);
default:
cout<<"wrong choice"; } }
int main()
{
s.switch_case();
return 0; }