0% found this document useful (0 votes)
145 views

CS Practical Class 12

This document contains a computer practical file with programs written in C++ on various topics like flow of control, functions, strings, and conditional statements. It includes 9 programs demonstrating basic concepts like half pyramid pattern, switch case, palindrome checking, function overloading, and more. Each program is commented and contains the source code to perform the given task. This is a collection of basic C++ programs made by Anmol Puri, a class 12 student, to learn common programming concepts.

Uploaded by

Beenu Puri
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

CS Practical Class 12

This document contains a computer practical file with programs written in C++ on various topics like flow of control, functions, strings, and conditional statements. It includes 9 programs demonstrating basic concepts like half pyramid pattern, switch case, palindrome checking, function overloading, and more. Each program is commented and contains the source code to perform the given task. This is a collection of basic C++ programs made by Anmol Puri, a class 12 student, to learn common programming concepts.

Uploaded by

Beenu Puri
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 60

COMPUTER

PRACTICAL FILE

MADE BY:BY:
MADE
ANMOL
AnmolPURI
Puri
CLASS : XII-D
CLASS XI-D
ROLL NO. : 07
Roll No. 07
INDE
X
Some basic programs(Flow of Control)

1.The C++ program to print the half pyramid using stars (*)
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int i, j;

for(i=0; i<5; i++)

for(j=0; j<=i; j++)

cout<<"* ";

cout<<"\n";

getch();

}
2.A C++ program using switch() case.
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n;

cin>>n;

switch(n)

case 1 : cout<<"monday";

break;

case 2 : cout<<"tuesday";

break;

case 3 : cout<<"wednesday";

break;

case 4 : cout<<"thursday";

break;

case 5 : cout<<"friday";

break;

default : cout<<"invalid value";

getch();

}
3.A C++ program to display the sum of the series from 1 to n.
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int i,n,sum=0;

cout<<“1+2+3+……+n”;

cout<<“Enter the value of n:”;

cin>>n;

for(i=1;i<=n;++i)

sum+=i;

cout<<“Sum=”<<sum;

getch();

4.A C++ program to check whether the no is palindrome or not.


#include <iostream.h>

#include<conio.h>

void main()

int n, num, digit, rev = 0;

cout << "Enter a positive number: ";

cin >> num;

n = num;
do

digit = num % 10;

rev = (rev * 10) + digit;

num = num / 10;

while (num != 0);

cout << " The reverse of the number is: " << rev << endl;

if (n == rev)

cout << " The number is a palindrome";

else

cout << " The number is not a palindrome";

getch();

5.Code to display digits of an entered number and their sum


#include<iostream.h>

#include<conio.h>

void main()

clrscr ();

int sum=0,n2=1,n1,n;

cout<<"Enter the Number"<<endl;

cin>>n;

cout<<endl<<"This number consists of the digits ";

while(n2!=0)
{

n1=n%10;

sum+=n1;

n2=n/10;

cout<<n1<<',';

n=n2;

cout<<endl<<"The sum of the digits contained in this number is "<<sum;

getch ();

6.Program to swap 2 numbers.


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int num1, num2, swap;

cout<<"Enter two number : ";

cout<<"\nFirst Number : ";

cin>>num1;

cout<<"Second Number : ";

cin>>num2;

swap=num1;

num1=num2;
num2=swap;

cout<<"The value of first and second number after swapping is \n";

cout<<"First Number = "<<num1<<"\n"<<"Second Number = "<<num2;

getch();

7.Program to check if the entered year is a leap year or not.


#include <iostream.h>

#include<conio.h>

void main()

int year;

cout << "Enter a year: ";

cin >> year;

if (year % 4 == 0)

if (year % 100 == 0)

if (year % 400 == 0)

cout << year << " is a leap year.";

else

cout << year << " is not a leap year.";

else

cout << year << " is a leap year.";

}
else

cout << year << " is not a leap year.";

getch();

8.Program to illustrate the use of conditional operator.


#include <iostream.h>

#include<conio.h>

void main()

int a ,b ,c ,greatest;

cout<<"Enter three numbers : ";

cin>>a>>b>>c;

greatest=(a>b&&a>c)?a:(b>c)?b : c;

cout<<"Greatest number is "<<greatest;

getch();

9.Program to display Fibonacci series.


#include <iostream.h>

#include<conio.h>

void main()

int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";

cin >> n;

cout << "Fibonacci Series: ";


for (int i = 1; i <= n; ++i)

/ Prints the first two terms.

if(i == 1)

cout << " " <<

t1; continue;

if(i == 2)

cout << t2 << "

"; continue;

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

cout << nextTerm << " ";

getch();

Strings
1.Program to know the length of the strings
#include<iostream.h>

#include<conio.h>
#include<stdio.h>

void main()

clrscr();

inti;

char s[80];

cout<<"Enter a string of max 79 letters"<<endl;

gets (s);

for(i=0;s[i]!='\0';i++);

cout<<" Total length of string is " <<i;

getch();

2.Program to check whether the entered 2 strings are equal or not.


#include<iostream.h>

#include <conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j,flag=0;

char s1[100],s2[100];

cout<<"Enter 1st string"<<endl;

gets(s1);

cout<<"Enter 2nd string"<<endl;

gets(s2);
for(i=0;s1[i]!='\0',s2[i]!='\0';i++)

if (s1[i]!=s2[i])

flag=1;

break;

if (flag==1)

cout<<"These strings are not equal"<<endl;

else

if (flag==0)

cout<<"These strings are equal"<<endl;

getch();

3.Program to print the reverse of the string.


#include<iostream.h>#include <conio.h>#include<stdio.h>

void main()

clrscr();

int i,j; char s[100],temp;


cout<<"Enter a charcter string"<<endl;

gets(s);

for(i=0;s[i]!='\0';i++);

j=i-1;

i=0;

while(i<j)

temp=s[i];

s[i]=s[j];

s[j]=temp;

--j;

++i;

cout<<"The reversed string is :- "<<endl;

puts(s);

getch();

4.Program to check whether the entered string is palindrome or not.


#include<iostream.h>

#include <conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j,flag=0;
char temp,s[100];

cout<<"Enter a string" <<endl;

gets(s);

for(i=0;s[i]!='\0';i++);

j=i-1;

i=0;

while(i<j)

if (s[i]!=s[j])

flag=1;

break;

--j;

++i;

if (flag==0)

cout<<"The string is a palindrome"<<endl;

else

if(flag==1)

cout<<"The string is not a palindrome"<<endl;

}
getch();

5.Program to print the concatenation of the 2 strings.


#include<iostream.h>

#include <conio.h>

#include<stdio.h>

void main()

clrscr();

int i,j;

char s1[100],s2[100],s3[100];

cout<<"Enter 1st string"<<endl;

gets(s1);

cout<<"enter the second string"<<endl;

gets(s2);

for(i=0;s1[i]!='\0';i++)

s3[i]=s1[i];

for(j=0;s2[j]!='\0';j++)

s3[i+j]=s2[j];

s3[i+j]='\0';

cout<<"The concatenated string is :"<<endl;


puts(s3);

getch();

6.Program to swap two strings.


#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i=0, j=0, k=0;

char str1[20], str2[20], temp[20];

cout<<"Enter the First String : ";

gets(str1);

cout<<"Enter the Second String : ";

gets(str2);

cout<<"Strings before swapping are :\n";

cout<<"String 1 = "<<str1<<"\n";

cout<<"String 2 = "<<str2<<"\n";

while(str1[i]!='\0')

temp[j]=str1[i];

i++;

j++;

}
temp[j]='\0';

i=0, j=0;

while(str2[i]!='\0')

str1[j]=str2[i];

i++;

j++;

str1[j]='\0';

i=0, j=0;

while(temp[i]!='\0')

str2[j]=temp[i];

i++;

j++;

str2[j]='\0';

cout<<"Strings after swapping : \n";

cout<<"String 1 = "<<str1<<"\n";

cout<<"String 2 = "<<str2<<"\n";

getch();

Functions
1.A C++ program to explain function overloading.
#include<iostream.h>

#include<conio.h>

/* Function arguments are of different data type */

long add(long, long);

float add(float, float);

int main()

long a, b, x;

float c, d, y;

cout << "Enter two integers\n";

cin >> a >> b;

x = add(a, b);

cout << "Sum of integers: " << x << endl;

cout << "Enter two floating point numbers\n";

cin >> c >> d;

y = add(c, d);

cout << "Sum of floats: " << y << endl;

return 0;

long add(long x, long y)

long sum;

sum = x + y;

return sum;
}

float add(float x, float y)

float sum;

sum = x + y;

return sum;

2.Code to generate 10 random nos. between 10 and 100 using rand()


function.
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

void main()

clrscr ();

int i;

cout<<"10 random numbers between 10 and 100 are "<<endl;

for(i=0;i<10;i++)

cout<<(rand()%10)+10<<endl; //One Random number generated and displayed

getch ();

3.Code to generate 10 random nos. between 10 and 100 using random


function
#include<iostream.h>

#include<conio.h>
#include<stdlib.h>

void main()

clrscr ();

int i;

cout<<"10 random numbers between 10 and 100 are "<<endl;

randomize();

for(i=0;i<10;i++)

cout<<random(91)+90<<endl;

//One Random number generated and displayed

getch ();

4.Code to swap values of two variables using ‘call by reference’


method of passing parameters
#include<iostream.h>

#include<conio.h>

void main()

clrscr ();

void swap(int&,int&); //Prototyping

inta,b;

cout<<"Enter two numbers"<<endl;

cin>>a>>b;

swap(a,b); //Invokation

cout<<"a= "<<a<<" and b= "<<b<<endl;


getch();

void swap(int& x, int& y) //Definition

int temp;

cout<<"Before swapping"<<endl<<"x= "<<x<<"y= "<<y<<endl;

temp=x;

x=y;

y=temp;

cout<<"After swapping"<<endl<<"x= "<<x<<"y= "<<y<<endl;

5.Code to swap values of two variables using ‘call by value’ method of


passing parameters
#include<iostream.h>

#include<conio.h>

void main()

clrscr ();

void swap(int,int); //Prototyping

int a,b;

cout<<"Enter two numbers"<<endl;

cin>>a>>b;

swap(a,b); //Invokation

cout<<"a= "<<a<<" and b= "<<b<<endl;

getch();
}

void swap(int x, int y) //Definition

int temp;

cout<<"Before swapping"<<endl<<"x= "<<x<<"y= "<<y<<endl;

temp=x;

x=y;

y=temp;

cout<<"After swapping"<<endl<<"x= "<<x<<"y= "<<y<<endl;

Arrays
1.FINDING ODD AND EVEN ELEMENTS
#include<iostream.h>

#include<conio.h>

void main( )

int a[100],size;

int e[100],o[100]; //e is for array of even elements and o for odd int

i=0,j=0;

cout<<”Enter the size of array”<<endl;

cin>>size;

cout<<”Enter the “<<size<<” elements”<<endl;

for(int x=0;x<size;x++)

cin>>a[x];
for(x=0;x<size;x++)

if(a[x]%2==0)

e[i++]=a[x];

else

o[j++]=a[x];

cout<<”The Even elements are: “;

for(int k=0;k<i;k++)

cout<<e[k]<<’\t’;

cout<<endl;

cout<<”The odd elements are: “;

for(int l=0;l<j;l++)

cout<<o[l]<<’\t’;

getch( );

2.FINDING AN ELEMENT IN AN ARRAY


#include<iostream.h>

#include<conio.h>

void main( )

clrscr( );

int a[100];

cout<<”Enter size”;

int size;
cin>>size;

cout<<”Enter the elements”<<endl;

for(int x=0;x<size;x++)

cin>>a[x];

int t;

cout<<”Enter the value to find”<<endl;

cin>>t;

for(x=0;x<size;x++)

if(a[x]==t)

cout<<”Element is present in the array”<<endl;

if(x==size)

cout<<”Element is not present in the given

array”<<endl; getch( );

3.SORTING AN ARRAY BY BUBBLE SORTING


#include<iostream.h>

#include<conio.h>

void main( )

clrscr( );

int a[20],size;

cout<<"Enter size: ";

cin>>size;
for(int i=0;i<size;++i)

cin>>a[i];

clrscr();

cout<<"Before sorting: \n";

for(i=0;i<size;++i)

cout<<a[i]<<" ";

for(i=0;i<size-1;++i)

int k=0;

for(int j=0;j<size-i-1;++j)

if(a[j]>a[j+1])

int t=a[j];

a[j]=a[j+1];

a[j+1]=t;

k++;

if(k==0)

break;

cout<<"\n\nAfter sorting: \n";

for(i=0;i<size;++i)

cout<<a[i]<<" ";
getch( );

4.FINDING LARGEST, SECOND LARGEST AND SMALLEST ELEMENTS


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"Enter 10 numbers : \n";

int n[10];

for(int i=0;i<10;++i)

cin>>n[i];

clrscr();

for(i=0;i<10;++i)

cout<<n[i]<<" ";

int l,sl,s;

for(i=0;i<10;++i)

int x=n[i];

if(i==0)

s=x;

l=x;

sl=0;

}
else if(i==1&&x<l)

sl=x;

s=x;

else if(x>l)

sl=l;

l=x;

else if(x>sl)

sl=x;

else if(x<s)

s=x;

cout<<"\n Largest : "<<l;

cout<<"\n Second Largest : "<<sl;

cout<<"\n Smallest : "<<s;

getch();

5.Program to increment even and odd no.s


#include<iostream.h>

#include<conio.h>

void main()

{
clrscr();

int arr[3][3], i, j; //incrementing even ele by 2 & odd by 3

cout<<"enter the elements of the 2-d array"<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>arr[i][j];

cout<<"the entered elements are: ";

cout<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<arr[i][j];

cout<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(arr[i][j]%2==0)
arr[i][j]+=2;

else

arr[i][j]+=3;

cout<<endl;

cout<<"the modified array is: ";

cout<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<arr[i][j]<<" ";

getch();

6.Program to display upper and lower triangle elements.


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int arr[3][3], i, j; //displaying lower & upper triangle elements

cout<<"enter the elements of the 2-d array: "<<endl;


for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>arr[i][j];

cout<<"the elements entered are: ";

cout<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<arr[i][j];

cout<<endl;

cout<<"the lower triangle elements are: "<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(i>j)

cout<<arr[i][j]<<endl;

}
}

cout<<"the upper triangle elements are: "<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(i<j)

cout<<arr[i][j]<<endl;

getch();

7.Program to display sum and average of 1d array.


#include<iostream.h>

#include<conio.h>

void main()

int Arr[100] ,n, i, sum=0;

cout<<"Enter number of elements you want to insert ";

cin>>n;

for(i=0;i<n;i++)

cout<<"Enter element "<<i+1<<":";

cin>>Arr[i];
}

for(i=0;i<n;i++)

sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;

cout<<"\nThe average of Array is :"<<sum/i;

getch();

8.program to display sum of 2 matrices.


#include <iostream.h>

#include<conio.h>

int main()

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

cout << "Enter number of rows (between 1 and 100): ";

cin >> r;

cout << "Enter number of columns (between 1 and 100):

"; cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

/ Storing elements of first matrix entered by user.

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << "Enter element a" << i + 1 << j + 1 << " : ";

cin >> a[i][j];

}
// Storing elements of second matrix entered by user.

cout << endl << "Enter elements of 2nd matrix: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << "Enter element b" << i + 1 << j + 1 << " : ";

cin >> b[i][j];

/ Adding Two matrices

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

sum[i][j] = a[i][j] + b[i][j];

/ Displaying the resultant sum matrix.

cout << endl << "Sum of two matrix is: " << endl;

for(i = 0; i < r; ++i)

for(j = 0; j < c; ++j)

cout << sum[i][j] << " ";

if(j == c - 1)

cout << endl;

return 0;

9.program to display multiplication of 2 matrices.


#include <iostream.h>

#include<conio.h>

int main()

int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j,

k; cout << "Enter rows and columns for first matrix: ";

cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";

cin >> r2 >> c2;

/ If column of first matrix in not equal to row of second matrix,

/ ask the user to enter the size of matrix again.

while (c1!=r2)

cout << "Error! column of first matrix not equal to row of second.";

cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";

cin >> r2 >> c2;

// Storing elements of first matrix.

cout << endl << "Enter elements of matrix 1:" << endl;

for(i = 0; i < r1; ++i)

for(j = 0; j < c1; ++j)

cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];

// Storing elements of second matrix.

cout << endl << "Enter elements of matrix 2:" << endl;

for(i = 0; i < r2; ++i)

for(j = 0; j < c2; ++j)

cout << "Enter element b" << i + 1 << j + 1 << " : ";

cin >> b[i][j];

/ Initializing elements of matrix mult to

0. for(i = 0; i < r1; ++i)

for(j = 0; j < c2; ++j)

mult[i][j]=0;

/ Multiplying matrix a and b and storing in array mult.

for(i = 0; i < r1; ++i)

for(j = 0; j < c2; ++j)

for(k = 0; k < c1; ++k)

mult[i][j] += a[i][k] * b[k][j];

/ Displaying the multiplication of two matrix.

cout << endl << "Output Matrix: " << endl;


for(i = 0; i < r1; ++i)

for(j = 0; j < c2; ++j)

cout << " " << mult[i][j];

if(j == c2-1)

cout << endl;

return 0;

10.Program to transpose a matrix.


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int arr[3][3], i, j, arrt[3][3];

cout<<"Enter 3*3 Array Elements : ";

for(i=0; i<3; i++)

for(j=0; j<3; j++)

cin>>arr[i][j];

cout<<"Transposing Array...\n";
for(i=0; i<3; i++)

for(j=0; j<3; j++)

arrt[i][j]=arr[j][i];

cout<<"Transpose of the Matrix is :\n";

for(i=0; i<3; i++)

for(j=0; j<3; j++)

cout<<arrt[i][j];

cout<<"\n";

getch();

Structures
1.program to illustrate nested structures.
#include<iostream.h>

#include<conio.h>

#include<stdio.h>
struct data

char name[25];

char section;

} ns;

struct student

int rollno;

int semester;

int age;

data ns;

};

void main( )

clrscr();

student genius;

cout<<"\n -------- Use of structure --------- \n"<<endl;

cout<<"\t Enter the Name = ";

gets(genius.ns.name);

cout<<"\t Enter the Roll No = ";

cin>>genius.rollno;

cout<<"\t Enter the Section = ";

cin>>genius.ns.section;

cout<<"\t Enter the Semester = ";


cin>>genius.semester;

cout<<"\t Enter the Age = ";

cin>>genius.age;

clrscr();

cout<<"\n -------- Use of Nested Structure --------- \n"<<endl;

cout<<"\t Name = "<<genius.ns.name<<endl; cout<<"\t Roll

No = "<<genius.rollno<<endl;

cout<<"\t Section = "<<genius.ns.section<<endl;

cout<<"\t Semester = "<<genius.semester<<endl;

cout<<"\t Age = "<<genius.age<<endl;

getch();

2.program to illustrate the passing of structures into functions.

i. call by val
#include <iostream.h>

#include<conio.h>

struct Person

char name[50];

int age;

float salary;

};

void displayData(Person); // Function declaration

void main()

{
Person p;

cout << "Enter Full name: ";

cin.get(p.name, 50);

cout << "Enter age: ";

cin >> p.age;

cout << "Enter salary: ";

cin >> p.salary;

/ Function call with structure variable as an argument

displayData(p);

getch();

void displayData(Person p)

cout << "\nDisplaying Information." << endl;

cout << "Name: " << p.name << endl;

cout <<"Age: " << p.age << endl;

cout << "Salary: " << p.salary;

ii. call by ref


#include<iostream.h>

#include<conio.h>

struct box

int height;

int weight;
int length;

};

void main()

clrscr();

void f(struct box &);

struct box b1,b2;

f(b1);

cout<<"Height is :"<<b1.height<<endl;

cout<<"Weight is :"<<b1.weight<<endl;

cout<<"Length is :"<<b1.length<<endl;

f(b2);

cout<<"Height is :"<<b2.height<<endl;

cout<<"Weight is :"<<b2.weight<<endl;

cout<<"Length is :"<<b2.length<<endl;

void f(struct box &a1)

cin>>a1.height;

cin>>a1.weight;

cin>>a1.length;

}
Inheritance
1.Program to show Single inheritance:-

#include<iostream.h>
#include<conio.h>
using namespace std;

class staff
{
private:
char name[50];
int code;
public:
void getdata();
void display();
};

class typist: public staff


{
private:
int speed;
public:
void getdata();
void display();
};

void staff::getdata()
{
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}

void staff::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
}

void typist::getdata()
{
cout<<"Speed:";
cin>>speed;
}
void typist::display()
{
cout<<"Speed:"<<speed<<endl;
}

int main()
{
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
getch();
return 0;
}

2.Program to show Multiple inheritance:-

#include <iostream>
#include <conio.h>
using namespace std;
class A
{
public:
void display()
{
cout <<"This is method of A";
}
};

class B
{
public:
void display()
{
cout <<"This is method of B";
}
};

class C: public A, public B

{
public:
};
int main()
{
C sample;
sample.display(); /*causes ambiguity*/
return 0;
getch();
}
3.Program to show Multilevel inheritance:-

#include <iostream>
#include <conio.h>
using namespace std;

class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};

class employee: public person


{
char company[100];
float salary;
public:
void getdata()
{
person::getdata();
cout<<"Name of Company: ";
fflush(stdin);
gets(company);
cout<<"Salary: Rs.";
cin>>salary;
}
void display()
{
person::display();
cout<<"Name of Company: "<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};

class programmer: public employee


{
int number;
public:
void getdata()
{
employee::getdata();
cout<<"Number of programming language known: ";
cin>>number;
}
void display()
{
employee::display();
cout<<"Number of programming language known: "<<number;
}
};

int main()
{
programmer p;
cout<<"Enter data"<<endl;
p.getdata();
cout<<endl<<"Displaying data"<<endl;
p.display();
getch();
return 0;
}

4.Program to show Hierarchical inheritance:-


#include <iostream>
#include <conio.h>

using namespace std;

class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};

class student: public person


{
char institute[100], level[20];
public:
void getdata()
{
person::getdata();
cout<<"Name of College/School: ";
fflush(stdin);
gets(institute);
cout<<"Level: ";
cin>>level;
}
void display()
{
person::display();
cout<<"Name of College/School: "<<institute<<endl;
cout<<"Level: "<<level<<endl;
}
};

class employee: public person


{
char company[100];
float salary;
public:
void getdata()
{
person::getdata();
cout<<"Name of Company: ";
fflush(stdin);
gets(company);
cout<<"Salary: Rs.";
cin>>salary;
}
void display()
{
person::display();
cout<<"Name of Company: "<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};

int main()
{
student s;
employee e;
cout<<"Student"<<endl;
cout<<"Enter data"<<endl;
s.getdata();
cout<<endl<<"Displaying data"<<endl;
s.display();
cout<<endl<<"Employee"<<endl;
cout<<"Enter data"<<endl;
e.getdata();
cout<<endl<<"Displaying data"<<endl;
e.display();
return 0;
getch();
}

Data Structures

1.Program to show stack:-

#include<iostream.h>
using namespace std;

#define MAX 1000

class Stack
{
int top;
public:
int a[MAX]; //Maximum size of Stack

Stack() { top = -1; }


bool push(int x);
int pop();
bool isEmpty();
};

bool Stack::push(int x)
{
if (top >= MAX)
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x;
return true;
}
}

int Stack::pop()
{
if (top < 0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int x = a[top--];
return x;
}
}

bool Stack::isEmpty()
{
return (top < 0);
}

// Driver program to test above functions


int main()
{
struct Stack s;
s.push(10);
s.push(20);
s.push(30);

cout << s.pop() << " Popped from stack\n";

return 0;
}

2.Program to show queue:-


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include<iostream.h>

// A structure to represent a queue


struct Queue
{
int front, rear, size;
unsigned capacity;
int* array;
};

// function to create a queue of given capacity.


// It initializes size of queue as 0
struct Queue* createQueue(unsigned capacity)
{
struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1; // This is important, see the enqueue
queue->array = (int*) malloc(queue->capacity * sizeof(int));
return queue;
}

// Queue is full when size becomes equal to the capacity


int isFull(struct Queue* queue)
{ return (queue->size == queue->capacity); }

// Queue is empty when size is 0


int isEmpty(struct Queue* queue)
{ return (queue->size == 0); }

// Function to add an item to the queue.


// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)%queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", item);
}

// Function to remove an item from queue.


// It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)%queue->capacity;
queue->size = queue->size - 1;
return item;
}

// Function to get front of queue


int front(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}

// Function to get rear of queue


int rear(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}

// Driver program to test above functions./


int main()
{
struct Queue* queue = createQueue(1000);

enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);

printf("%d dequeued from queue\n", dequeue(queue));

printf("Front item is %d\n", front(queue));


printf("Rear item is %d\n", rear(queue));

return 0;
}

3.Program to show circular queue:-

#include <iostream.h>
#define MAX 5
using namespace std;
/*
* Class Circular Queue
*/
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = new int [MAX];
rear = front = -1;
}
/*
* Insert into Circular Queue
*/
void insert(int item)
{
if ((front == 0 && rear == MAX-1) || (front == rear+1))
{
cout<<"Queue Overflow \n";
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item ;
}
/*
* Delete from Circular Queue
*/
void del()
{
if (front == -1)
{
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
/*
* Display Circular Queue
*/
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout<<"Queue is empty\n";
return;
}
cout<<"Queue elements :\n";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout<<cqueue_arr[front_pos]<<" ";
front_pos++;
}
}
cout<<endl;
}
};
/*
* Main
*/
int main()
{
int choice, item;
Circular_Queue cq;
do
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the element for insertion in queue : ";
cin>>item;
cq.insert(item);
break;
case 2:
cq.del();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}/*End of switch*/
}
while(choice != 4);
return 0;
}

4.Program to show linked lists:-


#include <stdlib.h>
#include <stdio.h>
#include<iostream.h>

// A linked list (LL) node to store a queue entry


struct QNode
{
int key;
struct QNode *next;
};

// The queue, front stores the front node of LL and rear stores ths
// last node of LL
struct Queue
{
struct QNode *front, *rear;
};

// A utility function to create a new linked list node.


struct QNode* newNode(int k)
{
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}

// A utility function to create an empty queue


struct Queue *createQueue()
{
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

// The function to add a key k to q


void enQueue(struct Queue *q, int k)
{
// Create a new LL node
struct QNode *temp = newNode(k);

// If queue is empty, then new node is front and rear both


if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}

// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}

// Function to remove a key from given queue q


struct QNode *deQueue(struct Queue *q)
{
// If queue is empty, return NULL.
if (q->front == NULL)
return NULL;

// Store previous front and move front one node ahead


struct QNode *temp = q->front;
q->front = q->front->next;

// If front becomes NULL, then change rear also as NULL


if (q->front == NULL)
q->rear = NULL;
return temp;
}

// Driver Program to test anove functions


int main()
{
struct Queue *q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
struct QNode *n = deQueue(q);
if (n != NULL)
printf("Dequeued item is %d", n->key);
return 0;
}
5.Program to show implementation of class:-
#include <iostream>
#include<conio.h>
using namespace std;

// Class Declaration

class person {
//Access - Specifier
public:

//Variable Declaration
string name;
int number;
};

//Main Function

int main() {
// Object Creation For Class
person obj;

//Get Input Values For Object Varibales


cout << "Enter the Name :";
cin >> obj.name;

cout << "Enter the Number :";


cin >> obj.number;

//Show the Output


cout << obj.name << ": " << obj.number << endl;

getch();
return 0;

6.Program to show implementation of constructor and destructor in a


class:-
#include<iostream.h>
#include<conio.h>

Class student
{

Private:

Char name [25];

Int roll;
Float height, weight;

Public:

Student ()

Strcpy (name, “ram”);

roll=0;

height=0;

weight=0;

Void display ()

Cout<<”\n name :”<< name;

Cout<<”\n roll no”<<roll;

Cout<<”\n Height”<<height;

Cout<<”\n weight”<<weight;

~ Student ()

Cout<<”\n destroying object”;

};

Void main ()

Student obj;

Obj. Student ();

getch ();
}

You might also like