0% found this document useful (0 votes)
24 views12 pages

CS Journal Final

The document contains code examples and explanations for 9 programming experiments involving classes and objects in C++. Experiment 1 sorts an array using bubble sort. Experiment 2 searches an array using binary search. Experiment 3 defines a Rectangle class to calculate area. Experiment 4 demonstrates constructor and destructor use. Experiment 5 defines class functions outside the class. Experiment 6 uses inheritance to calculate rectangle area from a Shape base class. Experiment 7 includes default and parameterized constructors.

Uploaded by

Himesh Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

CS Journal Final

The document contains code examples and explanations for 9 programming experiments involving classes and objects in C++. Experiment 1 sorts an array using bubble sort. Experiment 2 searches an array using binary search. Experiment 3 defines a Rectangle class to calculate area. Experiment 4 demonstrates constructor and destructor use. Experiment 5 defines class functions outside the class. Experiment 6 uses inheritance to calculate rectangle area from a Shape base class. Experiment 7 includes default and parameterized constructors.

Uploaded by

Himesh Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Page |1

CS I & II Reference Guide

Note: Kindly write the outputs on the blank page using “pencil” only.

Experiment #03
Aim: Write a program in C++ to sort an array of 10 integer numbers using the “bubble sort”
algorithm.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,j,c;
cout<<"Enter 10 numbers:";
for (i=0; i<10;i++)
{
cin>>a[i];
}
for (i=0;i<9;i++)
{
for (j=0; j<9;j++)
{
if (a[j]>a[j+1])
{
c=a [j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
cout<<”\nSorted Elements are:";
for(i=0;i<10;i++)
{
cout<<endl<<a[i];
}
getch();
}
Output:
Enter 10 numbers:

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |2
CS I & II Reference Guide
10
90
98
76
65
43
54
32
21
12
Sorted Elements are:
10
21
32
43
54
65
76
90
98

Experiment #04
Aim: Write a program in C++ to initialize an array of 10 integer numbers. Search for a number
from this list using the “binary search” algorithm.

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

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |3
CS I & II Reference Guide
void main()
{
int a[10], i, j, x, low=0, high=9, mid, pos=-1, temp,mid;
cout<<”Enter 10 numbers:”;
for(i=0;i<10;i++)
{
cin>>a[i];
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout<<”\nSorted Elements are:”<<endl;
for(i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
while(low<=high)
{
mid=(low+high)/2;

if(x==a[mid])
{
cout<<"\nElement found at position:"<<mid+1;
break;
}

else if(x>a[mid])
{
low=mid+1;
}

else

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |4
CS I & II Reference Guide
{
high=mid-1;
}
}

if(low>high)
{
cout<<"\nElement not found!";
}
getch();
}
Output:
Enter 10 numbers:
10
90
98
76
65
43
54
32
21
12
Sorted Elements are:
10
21
32
43
54
65
76

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |5
CS I & II Reference Guide
90
98

Enter the number to be searched: 32


Element found at position: 3

Experiment #05
Aim: Write a C++ program to define a class called rectangle. Create member function called
area to calculate the area of rectangle. Use member variables as needed.

Code:
#include<iostream.h>
#include<conio.h>
class Rectangle Name of class
{ Opening of class
private: Access specifier
int length breadth;
public:
void getdata() Function definition
{
cout<<"Enter the length:”
cin>>length;
cout<<”\nEnter Breadth”;
cin>>breadth;
}
void area()
{
area=length *breadth;
cout<<"\nThe Area is = "<<area;
}
}; Closing class
void main() Execution of program start from here
{
clrscr();
Rectangle r1; Object r1 of class Rctangle is defined
r1.getdata(); getdata() function is called
r1.area(); area() function is called
getch();
}

Output:
Enter length: 10
Enter breadth: 20

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |6
CS I & II Reference Guide

Area is: 200

Experiment #06
Aim: Write a program in C++ to implement the working of Constructor and Destructor in a class.
Use messages such as “object is born”, “object in use” and “object dies” to demonstrate the
output.

Code:
#include<iostream.h>
#include<conio.h>
class Ratio
{
public:
Ratio()//constructor
{
cout<<"Object is born"<<endl;
}
void print()
{
cout<<"\nObject is in use"<<endl;
}
~Ratio()//destructor
{
cout<<"\nObject dies"<<endl;
}
};
void main()
{
clrscr();
{
Ratio r;
r.print();
}
getch();
}
Output:
Object is born
Object is in use
Object dies

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |7
CS I & II Reference Guide

Experiment #7
Aim: Write a program in C++ to implement class ratio. Define member functions like assign(),
convert() and invert() outside the class using scope resolution operator.
Code:
#include<iostream.h>
#include<conio.h>
class ratio
{
public:
int num, den,c;
private:
void assign(int n, int d)
{
num=n;
den=d;
}
void convert(); //Function declaration
void invert();
};
void ratio::convert() //Function definition
{
cout<<”The ratio is:”<<num<<”/”<<den;
}
void ratio::invert()
{

c=num;
num=den;
den=c;

cout<<”The ratio after inversion is:”<<num<<”/”<<den;


}
void main()
{
clrscr();
ratio r;
r.assign(7,22);

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |8
CS I & II Reference Guide

r.convert();
r.invert();
getch();
}
Output:
The ratio is: 7/22
The ratio after inversion is: 22/7

Experiment #8
Aim: Write a program in C++ to declare a class called Shape. Create another class called
Rectangle and inherit the functions of Shape to find area of a rectangle. Use member variables
as needed.
Code:
#include <iostream.h>
#include<conio.h>
class Shape //base class
{
public:
int l, b;
void getdata()
{ cout<<"Enter length:”;
cin>>l;
cout<<"\nEnter breadth:”;
cin>>b;
}
};
class Rectangle:public Shape //derived class
{
public:
void area()
{
cout<<"\nArea is:"<<l*b;
}
};
void main()
{
clrscr();
Rectangle r; //object creation of child class rectangle
r.getdata();
r.area();
getch();

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


Page |9
CS I & II Reference Guide

Output:
Enter length:10
Enter breadth:2
Area is: 20

Experiment #09
Aim: Write a program in C++ to calculate area and circumference of circle using class Circle and
include default constructor in it.
Code:
#include<iostream.h>
#include<conio.h>
class Circle
{
private:
float r;
public:
Circle() //default constructor
{
r=0.0;
}
Circle(float rad) //parameterized constructor
{
r=rad;
}
void area()
{
cout<<"\n Area ="<<(3.14*r*r);
}
void circ()
{
cout<<"\n Circumference ="<<(2*3.14*r*r);
}

void main()
{
clrscr();
Circle obj(4);
obj.area();

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


P a g e | 10
CS I & II Reference Guide

getch();
}
Output:
Area =50.24
Circumference=25.12

Experiment #10
Aim: Write a program in C++ for Implementation of Constructor Overloading (Inheritance
Constructor).

Code:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
Base() //base class default constructor
{
cout<<"\n Base class constructor";
}
};
class Derived : public Base
{
public :
Derived () //derived default constructor
{
cout<<"\n Derived class constructor";
}
Derived (int i) //derived parameterized constructor
{
cout<<"\n Value from main function: "<<i;
}
};
void main()
{
clrscr();
Base b1;
Derived d1;
Derived d2 (15);
getch();

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


P a g e | 11
CS I & II Reference Guide

}
Output:
Base class constructor
Base class constructor
Derived class constructor
Base class constructor
Value from main function: 15

Experiment #11
Aim: Write a program in C++ for printing start and end address of elements of an array along
with sum using pointer.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
float a[]={10.2,3.9,4.6,5.5,6.9,};
float *ptr,sum=0;
ptr=a;
clrscr();
cout<<"\nStarting address \t size \t ending address \t value of sum";
for(inti=0;i<5;i++)
{
sum=sum+*ptr;
cout<<"\n"<<ptr<<"\t\t"<<sizeof(*ptr)<<"\t";
ptr=ptr+1;
cout<<ptr<<"\t\t"<<sum;
}
getch();
}
Output:

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM


P a g e | 12
CS I & II Reference Guide

RJMD | SARDAR VALLABHBHAI PATEL JR COLLEGE OF SCI & COMM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy