Skip to main content

Easy C++ Programs

Program to display hello

#include<iostream.h>   //header file
#include<conio.h>      //header file
main()
{
clrscr();
cout<<"Hello";        //cout is used for output
getch();
}

Output of this program
Hello

Program to add two numbers

#include<iostream.h>
#include<conio.h>
main()
{
int num1=10,num2=20,sum;
clrscr();
sum=num1+num2;
cout<<"Sum="<<sum;
getch();
}

Output of this program
 Sum=30

Program to  Average calculate of 10 students

#include<iostream.h>
#include<conio.h>
main()
{
int student[10],sum=0,i,avg;
clrscr();
cout<<"Enter the 10 student marks "<<endl;
for(i=0;i<10;i++)
{
cin>>student[i];
}
for(i=0;i<10;i++)
{
sum=sum+student[i];
}
avg=sum/10;
cout<<"Average="<<avg;
getch();
}

Output of this program
Enter the 10 students marks
 10
 20
 30
 40
 50
 60
 70
 80
 90
100
Average=55

Program using pointer display address of  item

#include<iostream.h>
#include<conio.h>
main()
{
int items=5;
int *ptr;
clrscr();
ptr=&items;
cout<<ptr;
getch();
}


Output of this program

0*8f91fff4

Program using pointer display address and value of number

#include<iostream.h>
#include<conio.h>
main()
{
int num1=10;
int *ptr;
clrscr();
ptr=&num1;
cout<<"Address of num1="<<ptr<<endl;
cout<<"Value of num1="<<*ptr;
getch();
}


Output of this program

Address of num1=0*8f82fff4
Value of num1=10


Program using function add two numbers

#include<iostream.h>           //Header file
#include<conio.h>              //Header file
main()
{
int num1=40,num2=100;
clrscr();
void add(int,int);           //Function declaration

add(num1,num2);        //Function call
getch();
}
void add(int x,int y)    //Function definition
{
int sum=x+y;

cout<<"Sum="<<sum;     //Function does not return value
}
Output of this program

Sum=140

Program using pass by value technique

#include<iostream.h>
#include<conio.h>
main()
{
int num1=20,num2=100;
clrscr();
void swap(int,int);
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
swap(num1,num2);
getch();
}
void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping value is num1="<<x<<" and "<<"num2="<<y;
}

Output of this program

num1=20
num2=100
After swaping value is num1=100 and num2=20

Program using pass by address technique

 #include<iostream.h>
#include<conio.h>
main()
{
int num1=20,num2=100;
clrscr();
void swap(int *,int *);
cout<<"Num1="<<num1<<endl<<"Num2="<<num2<<endl;
swap(&num1,&num2);
cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
getch();
}
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
cout<<"After swaping x="<<*x<<" and "<<"y="<<*y<<endl;
}

Output of this program

num1=20
num2=100
After swaping value is x=100 and y=20
Afte r swaping num1=100 and num2=20


Program using pass by reference technique

#include<iostream.h>
#include<conio.h>
main()
{
int num1=20,num2=100;
clrscr();
cout<<"num1="<<num1<<endl<<"num2="<<num2<<endl;
void swap(int &,int &);
swap(num1,num2);
cout<<"After swaping num1="<<num1<<" and "<<"num2="<<num2;
getch();
}
void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
cout<<"After swaping x="<<x<<" and "<<"y="<<y<<endl;
}



Output of this program

num1=20
num2=100
After swaping  x=100 and y=20
After swaping num1=100 and num2=20

Create a class to store student information with data members as roll no, name, marks in 3subjects total and average using constructor where ever required.

            #include<iostream.h>
#include<conio.h>
class student
{
private:
 int rollnumber;
 char name[10];
 int  emarks,cppmarks,mmarks;
 int totalmarks,saverage;
public:
 student()
 {
 cout<<"Enter the student rollnumber ";
 cin>>rollnumber;
 cout<<"Enter the student name ";
 cin>>name;
 cout<<"Enter the student marks in English ";
 cin>>emarks;
 cout<<"Enter the student marks in C++ ";
 cin>>cppmarks;
 cout<<"Enter the student marks Discrete Mathematics ";
 cin>>mmarks;
 }
 void addmarks()
 {
 totalmarks=emarks+cppmarks+mmarks;
 }
 void average()
 {
 saverage=totalmarks/3;
 }
 void display()
 {
 cout<<"Student roll number "<<rollnumber<<endl<<"Student name "<<name<<endl;
 cout<<"Student English marks "<<emarks<<endl;
 cout<<"student C++ marks "<<cppmarks<<endl;
 cout<<"Student Discrete mathematics marks "<<mmarks<<endl;
 cout<<"Total marks "<<totalmarks<<endl<<"Average "<<saverage;
 }
};
main()
{
clrscr();
student s1;
s1.addmarks();
s1.average();
s1.display();
getch();
}
Output of this program


Enter the student roll number  1818
Enter the student name Johan
Enter the student marks in English 89
Enter the student marks in C++ 90
Enter the student marks in Discrete Mathematics 99
Student roll number 1818
Student name Johan
Student English marks 89
Student C++ marks 90
Student Discrete mathematics marks 99
Total marks 278
Average 92

Program to  using Abstract Data Type (ADT) to find largest and smallest elements in an array.

 

#include<iostream.h>
#include<conio.h>
class largesmall
{
private:
 int num[50],n;
public:
  largesmall()
  {
  cout<<"How many elements you want to insert an array ";
  cin>>n;
  }
  void setdata()
  {
  int i=1;
  for(i=1;i<=n;i++)
  {
  cout<<"Enter the "<<i<<" element ";
  cin>>num[i];
  }}
  void caculatelarge()
  {
  int i,max=0;
  for(i=1;i<=n;i++)
  {
  if(max<num[i])
  {
  max=num[i];
  }}
  cout<<"Large element is="<<max<<endl;
  }
  void calculatesmall()
  {
  int i,min;
  min=num[1];
  for(i=1;i<=n;i++)
  {
  if(min<=num[i])
  {}
  else
  {
  min=num[i];
  }}
  cout<<"Smallest element is="<<min;
  }

};
main()
{
clrscr();
largesmall ls1;
ls1.setdata();
ls1.caculatelarge();
ls1.calculatesmall();
getch();
}

Output of this program

How many elements you want to insert an array 4
Enter the 1 element  5
Enter the 1 element  3
Enter the 1 element  6
Enter the 1 element  1
Large  element is=6
Smallest element is=1


Program in C++ to implement Bubble sort and Selection Sort


#include<iostream.h>
#include<conio.h>
class bubleselection
{
private:
 int num[50],n,min,loc,i;
public:
 void setdata()
 {
 cout<<"Enter the number of elements you want to short ";
 cin>>n;
 for(i=1;i<=n;i++)
 {
 cout<<"Enter the "<<i<<" element ";
 cin>>num[i];
 }}
 void display()
 {
 setdata();
 int i=1;
 cout<<"Elements "<<endl;
 for(i=1;i<=n;i++)
 {
 cout<<num[i]<<"\t";
 }
 cout<<endl;
 }
 void bubleshort()
 {
 int i=1,j=1;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=n-1;j++)
 {
 if(num[j]>num[j+1])
 {
 int temp=num[j];
     num[j]=num[j+1];
     num[j+1]=temp;
 }
 }}
 cout<<"After bubble sort"<<endl;
 for(i=1;i<=n;i++)
 {
 cout<<num[i]<<"\t";
 }
 cout<<endl;
 }
 void selectionsort()
 {
 for(i=1;i<=n;i++)
 {
 loc=ret(i);
 int temp=num[i];
 num[i]=num[loc];
 num[loc]=temp;
 }
 cout<<"After selection sort"<<endl;
 for(i=1;i<=n;i++)
 {
 cout<<num[i]<<"\t";
 }
 }
 int ret(int k)
 {
 int j;
 i=k;
 min=num[i],loc=i;
 for(j=i;j<=n;j++)
 {
 if(min<=num[j])
 {
 }
 else
 {
 min=num[j];
 loc=j;
 }}
 return (loc);
 }
};
main()
{
clrscr();
bubleselection bs1;
bs1.display();
bs1.bubleshort();
bs1.selectionsort();
getch();
}
Output of this program

Enter the number of elements you want to sort 4
Enter the 1 element 10
Enter the 1 element  4
Enter the 1 element 25
Enter the 1 element 65
 Elements
 10         4         25         65
After bubble sort
 4           10       25        65 
After selection sort

 4           10       25        65 

program using ADT to perform linear search.

Program to using ADT to perform linar search

#include<iostream.h>
#include<conio.h>
class linearsearch
{
private:
 int num[50],n,i,search;
public:
 linearsearch()
 {
 cout<<"How many elements of an array you want to insert ";
 cin>>n;
 for(i=1;i<=n;i++)
 {
 cout<<"Enter the "<<i<<" element ";
 cin>>num[i];
 }}
 void caculate()
 {
 int temp,loc;
 cout<<"Enter the element you want to search ";
 cin>>search;
 for(i=1;i<=n;i++)
 {
 if(search==num[i])
 {
 temp=search;
 loc=i;
 cout<<"Element is search successfully "<<search<<endl;
 cout<<"Element Location is="<<loc;
 }}
 if(temp==num[loc])
 {}
 else
 {
 cout<<"Element is not in an array"<<endl;
 }
 }
};
main()
{
clrscr();
linearsearch s1;
s1.caculate();
getch();
}

Output of this program

How many elements of an array you want to insert 4
Enter the 1 element 10
Enter the 1 element  4
Enter the 1 element 25
Enter the 1 element 65
Enter the element you want to search 65
Element is search successfully 65
Element Location is=4


Program to using ADT to perform binary search.

#include<iostream.h>
#include<conio.h>
class search
{
private:
 int num[10],n;
public:
 search()
 {
 cout<<"How many element you want to insert in list ";
 cin>>n;
 for(int i=1;i<=n;i++)
 {
 cout<<"Enter the "<<i<<" element ";
 cin>>num[i];
 }}
 void binarysearch()
 {
 int item,beg=1,end=n,mid;
 cout<<"Enter the element you want to search ";
 cin>>item;
 mid=(beg+end)/2;
 for(int i=1;i<=n;i++)
 {
 if(item>num[mid] && item!=num[mid])
 {
 beg=mid+1;
 }
 else
 {
 if(item!=num[mid])
 {
 end=mid-1;
 }}
 mid=(beg+end)/2;
 }
 if(item==num[mid])
 {
 cout<<"Element search successfully "<<item<<endl;
 cout<<"Element Location is="<<mid;
 }
 else
 {
 cout<<"Element is not in list"<<endl;
 }
 }
};
main()
{
clrscr();
search s1;
s1.binarysearch();
getch();
}
Output of this program

How many element you want to insert in list 4

Enter the 1 element 10
Enter the 1 element 20
Enter the 1 element 30
Enter the 1 element 40
Enter the element you want to search 40
Element  search successfully 40
Element Location is=4

  

 Program to using ADT to add and subtract two matrices.

#include<iostream.h>
#include<conio.h>
class matrix
{
private:
 int a[10][10],b[10][10],c[10][10];
 static int n,m;
public:
 void setdata()
 {
 int i=1,j=1;
 cout<<"Enter the value of A matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cin>>a[i][j];
 }}
 cout<<"Enter the value of B matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cin>>b[i][j];
 }}

 }
 void display()
 {
 int i=1,j=1;
 cout<<"A matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<a[i][j];
 cout<<"\t";
 }
 cout<<endl;
 }
 cout<<"B matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<b[i][j]<<"\t";
 }
 cout<<endl;
 }

 }
 void add()
 {
 int i=1,j=1;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 c[i][j]=a[i][j]+b[i][j];
 }}
 cout<<"After addition matrix"<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<endl;
 }
 cout<<endl;
 }
 void sub()
 {
 int i=1,j=1;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 c[i][j]=a[i][j]-b[i][j];
 }}
 cout<<"After subtraction matrix"<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<endl;
 }
 cout<<endl;
 }
};
int matrix::n=2;
int matrix::m=3;
main()
{
clrscr();
matrix  m1;
m1.setdata();
m1.display();
m1.add();
m1.sub();
getch();
}
Output of this program
Enter the value of A matrix
100 

200
300
400
500
600
Enter the value of B matrix
10
20
30
40
50
60
A matrix
100       200      300
400       500      600 
B matrix
10         20        30
40         50        60
After addition matrix
110       220     330
440       550     660 
After subtraction matrix
90        180      270
360      450     540 


Program  to using ADT to Multiply and Transpose two matrices.

#include<iostream.h>
#include<conio.h>
class matrix
{
private:
 int a[10][10];
 int b[10][10],c[10][10],d[10][10];
 static int n,m;
public:
 void setdata()
 {
 int i,j;
 cout<<"Enter the value of A matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cin>>a[i][j];
 }}
 cout<<"Enter the value of B matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cin>>b[i][j];
 }
 }}
 void display()
 {
 int i,j;
 cout<<"A matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<a[i][j];
 cout<<"\t";
 }
 cout<<endl;
 }
 cout<<"B matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<b[i][j]<<"\t";
 }
 cout<<endl;
 }}
 void multiplication()
 {
 int i,j;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 int p=1;
 c[i][j]=a[i][p]*b[p][j];
 int t=2;
 d[i][j]=a[i][t]*b[t][j];
 }}
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 c[i][j]=c[i][j]+d[i][j];
 }}
 cout<<"After Multiplication  matrix "<<endl;
 for(i=1;i<=n;i++)
 {
 for(j=1;j<=m;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<endl;
 }
 }
 void transpose()
 {
 int i,j;
 cout<<"After transpose of matrix A "<<endl;
 for(i=1;i<=m;i++)
 {
 for(j=1;j<=n;j++)
 {
 c[i][j]=a[j][i];
 }
 }
 for(i=1;i<=m;i++)
 {
 for(j=1;j<=n;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<endl;
 }
 cout<<"After transpose of matrix B "<<endl;
 for(i=1;i<=m;i++)
 {
 for(j=1;j<=n;j++)
 {
 c[i][j]=b[j][i];
 }
 }
 for(i=1;i<=m;i++)
 {
 for(j=1;j<=n;j++)
 {
 cout<<c[i][j]<<"\t";
 }
 cout<<endl;
 }
 }
};
int matrix::n=2;
int matrix::m=2;
main()
{
matrix m1;
clrscr();
m1.setdata();
m1.display();
m1.multiplication();
m1.transpose();
getch();
}
Output of this program
Enter the value of A matrix
10


20
30
40
Enter the value of B matrix
1
2
3
4
A matrix
10       20    
30       40       
B matrix
1        2    
3        4 
After Multiplication matrix  


70       100   
150     220
After transpose of matrix A  
10       30
20       40
After transpose of matrix B 
1         3
2         4


Write a program to read 2 integers and perform simple arithmetic operations using pointer technique. (Use new and delete operators)
#include<iostream.h>
#include<conio.h>
class integerdynamic
{
private:
 int *num1,*num2;
public:
 integerdynamic()
 {
 num1=new int;
 num2=new int;
 }
 void setdata()
 {
 cout<<"Enter the number 1: ";
 cin>>*num1;
 cout<<"Enter the number 2: ";
 cin>>*num2;
 }
 void add()
 {
 int *sum=new int;
 *sum=*num1+*num2;
 cout<<"Addition of two numbers is="<<*sum<<endl;
 delete sum;
 }
 void sub()
 {
 int *sub=new int;
 *sub=*num1-*num2;
 cout<<"Subtraction of two numbers is="<<*sub<<endl;
 delete sub;
 }
 void mul()
 {
 int *mul=new int;
 *mul=*num1**num2;
 cout<<"Multiplication of two numbers is="<<*mul<<endl;
 delete mul;
 }
 void div()
 {
 int *div=new int;
 if(*num2==0)
 {
 cout<<*num1<<" is not divisle by "<<*num2<<endl;
 }
 else
 {
 *div=*num1/ *num2;
 cout<<"Divition of two numbers is="<<*div<<endl;
 }
 delete div;
 }
 void mod()
 {
 int *mod=new int;
 *mod=*num1%*num2;
 cout<<"Moduls of two numbers is="<<*mod<<endl;
 delete mod;
 }
 void del()
 {
 delete num1;
 delete num2;
 }
};
main()
{
int option,y;
integerdynamic id1;
clrscr();
do
{
cout<<"Press 1 for addition operation"<<endl;
cout<<"Press 2 for subtraction operation"<<endl;
cout<<"Press 3 for multiplication operation"<<endl;
cout<<"Press 4 for division operation"<<endl;
cout<<"Press 5 for modules operation"<<endl;
cout<<"Enter your option: ";
cin>>option;
switch(option)
{
case 1:
id1.setdata();
id1.add();
break;
case 2:
id1.setdata();
id1.sub();
break;
case 3:
id1.setdata();
id1.mul();
break;
case 4:
id1.setdata();
id1.div();
break;
case 5:
id1.setdata();
id1.mod();
break;
default:
{
cout<<"Please check the option"<<endl;
}
}
cout<<"Do you want to continue this program then press 1 ";
cin>>y;
}while(y==1);
id1.del();
getch();
}

Output of this program

Press 1 for addition operation

Press 2 for subtraction operation

Press 3 for multiplication operation

Press 4 for division operation

Press 5 for modules operation

Enter your option:1

Enter the number 1: 50

Enter the number 2: 10

Addition of two numbers is=60

 Do you want to continue this program the press 1 1

Press 1 for addition operation

Press 2 for subtraction operation

Press 3 for multiplication operation

Press 4 for division operation

Press 5 for modules operation

Enter your option:2

Enter the number 1: 50

Enter the number 2: 10

Subtraction of two numbers is=40

 Do you want to continue this program the press 1 1

Press 1 for addition operation

Press 2 for subtraction operation

Press 3 for multiplication operation

Press 4 for division operation

Press 5 for modules operation

Enter your option:3

Enter the number 1: 50

Enter the number 2: 10

Multiplication of two numbers is=500

 Do you want to continue this program the press 1 1

Press 1 for addition operation

Press 2 for subtraction operation

Press 3 for multiplication operation

Press 4 for division operation

Press 5 for modules operation

Enter your option:4

Enter the number 1: 50

Enter the number 2: 10

Division of two numbers is=5

 Do you want to continue this program the press 1 1

 

Press 1 for addition operation

Press 2 for subtraction operation

Press 3 for multiplication operation

Press 4 for division operation

Press 5 for modules operation

Enter your option:5

Enter the number 1:16

Enter the number 2:2

Modules of two numbers is=0

 Do you want to continue this program the press 1 1

Press 1 for addition operation

Press 2 for subtraction operation

Press 3 for multiplication operation

Press 4 for division operation

Press 5 for modules operation

Enter your option:5

Enter the number 1:17

Enter the number 2:2

Modules of two numbers is=1

 Do you want to continue this program the press 1 1

  Write a program to read an array and display an array using dynamic memory allocation.

#include<iostream.h>
#include<conio.h>
class array
{
private:
 int *a;
 int n;
public:
 array()
 {
 a=new int[50];
 cout<<"How many elements into array you want to insert ";
 cin>>n;
 }
 void setdata()
 {
 int i;
 for(i=1;i<=n;i++)
 {
 cout<<"Enter the  "<<i<<" element ";
 cin>>*(a+i);
 }}
 void display()
 {
 int i;
 cout<<"Presently available elements into an array"<<endl;
 for(i=1;i<=n;i++)
 {
 cout<<*(a+i)<<"\t";
 }
 delete[] a;
 }
};

main()
{
clrscr();
array a1;
a1.setdata();
a1.display();
getch();
}

Output of this program
How many elements into array you want to insert 4
Enter the 1 element  10
Enter the 2 element  20
Enter the 3 element  30 
Enter the 4 element  40
Presently available elements into an array
10     20     30     40 

Program to implement stack  ADT using array.

#include<iostream.h>
#include<conio.h>
class stack
{
private:
 int stk[50];
 int top,i,n;
 char y;
public:
 stack()
 {
 top=0;
 cout<<"How many element into stack ";
 cin>>n;
 }
 void push()
 {
 if(top==n)
 {
 cout<<"Stack is overflow"<<endl;
 }
 else
 {
 top=top+1;
 cout<<"Enter the element you want to insert ";
 cin>>stk[top];
 cout<<"Element "<<stk[top]<<" is successfully stored into stack"<<endl;
 }}
 void pop()
 {
 int item;
 if(top==0)
 {
 cout<<"Stack is under flow"<<endl;
 }
 else
 {
 item=stk[top];
 top=top-1;
 cout<<"Element "<<item<<" is delete(POP) into stack successfully"<<endl;
 }}
 void display()
 {
 cout<<"----------------------------"<<endl;
 cout<<"Elements in the stack"<<endl;
 cout<<endl;
 if(top!=0)
 {
 for(i=1;i<=top;i++)
 {
 cout<<stk[i]<<"\t";
 }}
 else
 {
 cout<<"No element into stack"<<endl;
 }
 cout<<endl;
 }
};
main()
{
int optn,y;
clrscr();
stack s1;
clrscr();
do
{
cout<<"Press 1 Insert the element into stack"<<endl;
cout<<"Press 2 Delete the element into stack"<<endl;
cout<<"Press 3 Display"<<endl;
cout<<"Enter the option: ";
cin>>optn;
switch(optn)
{
case 1:
s1.push();
break;
case 2:
s1.pop();
break;
case 3:
s1.display();
break;
default:
{
cout<<"Please check the option"<<endl;
}}
cout<<"Do you want to continue this program press 1: ";
cin>>y;
}while(y==1);
getch();
}


Output of this program
How many elements into stack 4
Press 1 Insert the element into stack
Press 2 Delete the element into stack
Press 3 Display
Enter the option: 1
Enter the element you want to insert 10
Element  10 is successfully stored into stack
Do you want to continue this program press 1:1
Press 1 Insert the element into stack
Press 2 Delete the element into stack
Press 3 Display
Enter the option: 1
Enter the element you want to insert 20
Element  20 is successfully stored into stack
Do you want to continue this program press 1:1
Press 1 Insert the element into stack
Press 2 Delete the element into stack
Press 3 Display
Enter the option:3
----------------------------
Elements in the stack
10  20
Do you want to continue this program press 1:1
Press 1 Insert the element into stack
Press 2 Delete the element into stack
Press 3 Display
Enter the option: 2
Element  20 is delete(POP) into stack successfully
Do you want to continue this program press 1:1
Press 1 Insert the element into stack
Press 2 Delete the element into stack
Press 3 Display
Enter the option:3
----------------------------
Elements in the stack
10
Do you want to continue this program press 1: 2

Write a program to create memory space for a class object using new operator and to destroy it using delete operator. 

#include<iostream.h>
#include<conio.h>
class object
{
public:
 object()
 {
 cout<<"Create memory space for a class object using new operator and destroy using delete operator"<<endl;
 }
};
main()
{
clrscr();
object *o1=new object;
delete o1;
getch();
}


Output of this program
Create memory space for a class object using new operator and destroy using delete operator

Develop an Object Oriented program in C++ to read emp name,emp code,designation,experience and age.Construct the database with suitable member functions for initializing and destroying the data using constructor and destructor and dynamic memory allocation new and delete.

#include<iostream.h>
#include<conio.h>
class empoloy
{
private:
 char *name;
 int *emp_code;
 char *designation;
 int *experience,*age;
public:
 empoloy()
 {
 name=new char[20];
 emp_code=new int;
 designation=new char[20];
 experience=new int;
 age=new int;
 cout<<"Enter the employee name ";
 cin.getline(name,20);
 cout<<"Enter the employee designation ";
 cin.getline(designation,20);
 cout<<"Enter the employee code ";
 cin>>*emp_code;

 cout<<"Enter the employee experience in years ";
 cin>>*experience;
 cout<<"Enter the employee age ";
 cin>>*age;
 }
 void display()
 {
 cout<<"Employee informination"<<endl;
 cout<<"Employee name "<<name<<endl;
 cout<<"Emp_code "<<*emp_code<<endl;
 cout<<"Employee designation "<<designation<<endl;
 cout<<"Employ experience in years"<<*experience<<endl;
 cout<<"Employee age "<<*age<<endl;
 }
 ~empoloy()
 {
 delete[] name;
 delete emp_code;
 delete[] designation;
 delete experience;
 delete age;
 }
};
main()
{
clrscr();
empoloy *e1=new empoloy;
e1->display();
delete e1;
getch();
}

Output of this program
 Enter the employee name Johan 
 Enter the employee designation Manger 
 Enter the employee code  145
 Enter the employee experience in years 5 
 Enter the employee age  40
Employee informination
 Employee name Johan 
 Emp_code  145
 Employee designation Manger 
 Employ experience  in years 5
 Employee age   40

Program in C++ to prepare mark sheet of an University exam by reading stuname,rollno,subname,subcode,internal marks,external marks>Desgin a base class consisting data members such as student name,roll no,sub name.Derived class consists data members such as sub code,interbal marks,external marks,construct oops data to search for a record i.e be printed.

#include<iostream.h>
#include<conio.h>
class uexam
{
public:
 char stu_name[20];
 int rollno;
 char sub_name[20];
public:
 void setdata()
 {
 cout<<"Enter the student name ";
 cin>>stu_name;
 cout<<"Enter the student roll number ";
 cin>>rollno;
 cout<<"Enter the subject name ";
 cin>>sub_name;
 }
 void display()
 {
 cout<<"----------------------------------------------------------------------------"<<endl;
 cout<<"Name"<<"\t"<<" Roll number"<<"\t"<<"Subject name"<<"\t";
 }
};
class report:public uexam
{
private:
 int sub_code,imarks,emarks;
public:
 void setdata()
 {
 uexam::setdata();
 cout<<"Enter the Sub_code ";
 cin>>sub_code;
 cout<<"Student internal marks ";
 cin>>imarks;
 cout<<"Student external marks ";
 cin>>emarks;
 }
 void display()
 {
 uexam::display();
 cout<<"Sub_code  "<<"internal marks   "<<"externalmarks"<<endl;
 cout<<"----------------------------------------------------------------------------"<<endl;
 cout<<stu_name<<"\t"<<rollno<<"\t"<<sub_name<<"\t"<<sub_code<<"\t"<<"\t"<<imarks<<"\t"<<"\t"<<emarks<<endl;
 cout<<"----------------------------------------------------------------------------"<<endl;
 }
};
main()
{
clrscr();
report r1;
r1.setdata();
r1.display();
getch();
}


  Output of this Program
  Enter the student name  Narinder
  Enter the student roll number 1200
  Enter the subject name  LearnC++
  Enter the Sub_code   121
  Student internal marks 89
  Student external marks 99
--------------------------------------------------------------------------------------------------------------------------
Name          Roll number        Subject name      Sub_code        Internal marks       External marks
-------------------------------------------------------------------------------------------------------------------------- 
Narinder         1200                 LearnC++           121                  89                          99
-------------------------------------------------------------------------------------------------------------------------- 




 

 

 

 

 

 


 

 


Comments

Post a Comment

Popular posts from this blog

Generation of Computers

Generation of Computers   – Computers were developed in different phases known as generations of computer. Depending upon the technologies used the development of electronic computers can be divided into five generations. 1.  First generation The duration lasted from 1946-1959 was based on  vacuum tubes . Because thousands of such bulbs were used, the computers were very large and generate a large amount of heat, causing many problems in temperature regulation.  Magnetic drums   were used for  memory  purpose and instruction and data was given through  punch cards . Computer were operated manually and instruction given in  machine language . E.g.   –   UNIVAC  (Universal automatic computer),  ENIAC  (Electronic Numerical Integrator And Calculator ) ,  Mark I  etc. Main Features  – 1.        Vacuum tube technology used 2.     ...

Input and Output devices

Input device Input device is a device through which data and instruction are entered into computer system. An input devices converts the data and instructions into binary form that computer can understand. This transformation is performed by “Input interface”.   The data entered through input device can be some text, some graphical image or symbol, sound etc, depending on the form of the raw data the various input devices are available. Basic Function Performed by Input unit of a computer system -   1. It accepts the instruction and data from the user. 2. It converts these instruction and data in computer acceptable form. 3. It supplies the converted instruction and data to the computer system for further processing. Some of the commonly input devices used are:- 1. Keyboard 2. Mouse 3. Joy stick 4. Track ball 5. Touch screen 6. Light Pen 7. Digitizer 8. Scanner 9. Speech Recognition Devices 1. Keyboard Keyboard is an input device for enteri...

Computer Memory

Memory :  A memory is just like a human brain. It is used to store data and instructions. Computer memory is the storage space in computer where data is to be processed and instructions required for processing are stored. The memory is divided into large number of small parts. Each part is called cell. Each location or cell has a unique address, which varies from zero to memory size minus one. The computer storage memory is measure in term of Bytes. Eight bits make one Bytes. (Measure units)   Primary Memory/Main Memory Primary memory holds only those data and instructions on which computer is currently working. Has limited capacity and data gets lost when power is switched off. It is also called main memory. It is generally made up of semiconductor device. These memories are not as fast as registers. The data and instructions required to be processed earlier reside in main memory. Characteristic of Main Memory ·         ...