Programming using C++
Ex. No. : 1 / Functions with default arguments and call by value, address, referenceAIM:
To write a C++ program using functions with default arguments and call by value, address, reference.
ALGORITHM:
Step 1: Defining the variable and function for cube, cuboids and cylinder.
Step 2: Declaring the function with one argument for cube and return type as integer.
Step 3: Declaring the function with three argument length, breadth, and height for
cuboids with return type double.
Step 4: Declaring the function with two argument radius and height for cylinder with
return type double.
Step 5: Calling the declared function with parameter value for cube, cuboid and
cylinder.
Step 6: Print the return value.
Step 7: Stop the process.
PROGRAM:
#include<iostream.h>
#include<conio.h>
double pi=3.14;
doublevolumeofcube (double a)
{
return (a*a*a);
}
doublevolumeofCuboids (double *l, double *b, int h=9)
{
return ((*l)*(*b)*h);
}
doublevolumeofCylinder (double &r, int h=8)
{
return (pi*r*r*h);
}
void main()
{
double r;
cout<"\n Enter the radius of the cube:";
cin>r;
cout<"Volume of Cube :"<volumeofcube(r)<endl;
doublel,b;
cout<"\n Enter the length and breath of the Cuboid :";
cin>l>b;
cout<"Volume of Cuboid :"<volumeofCuboids(&l,&b)<endl;
cout<"\n Enter the radius of the Cylinder :";
cin>r;
cout<"Volume of Cylinder:"<volumeofCylinder(r)<endl;
}
OUTPUT:
RESULT:
Thus the implementation of functions with default arguments and call by value, address, referenceare executed successfully.
Ex. No. : 2 / simple classes for understanding objects, member functions & constructorsAIM:
To write a C++ program using objects, member functions & constructors.
ALGORITHM:
Step 1: Start the program.
Step2: Include necessary header files.
Step 3: Create class student with data members.
Step 4: Declare static member function.
Step 5: Declare show function to display the result
Step 6: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
private:
static const int maxmark;
public:
int *v;
int sz;
char name[20];
staticintobjcount;
student()
{
objcount++;
}
voidsubjectcount(int size)
{
sz = size;
v = new int[size];
}
void read()
{
cout<"Enter Student Name";
cin>name;
int mark = 0;
for(int i=0;i<sz;i++)
{
constantrain:
cout<"Enter mark for subject ["<i+1<"]";
cin>mark;
if(mark>maxmark)
{
cout<"Max should be less than or equal to "<maxmark;
gotoconstantrain;
}
else
v[i]=mark;
}
}
void show()
{
int sum = 0;
cout<"\nStudent Roll Number: "<objcount;
cout<"\nStudent Name : " <name;
for(int i=0;i<sz;i++)
{
sum += v[i];
}
cout<"Sum of Marks = " < sum;
}
void release()
{
delete v;
}
};
const int student::maxmark=100;
int student::objcount = 0;
void main()
{
student s1;
int count;
cout<"Enter the Number of Subjects";
cin>count;
s1.subjectcount(count);
s1.read();
s1.show();
s1.release();
student s2;
s2.subjectcount(count);
s2.read();
s2.show();
s2.release();
student s3;
s3.subjectcount(count);
s3.read();
s3.show();
s3.release();
}
OUTPUT:
RESULT:
Thus the implementation of objects, member functions &constructorsare executed successfully.
Ex. No. :3(A) / Operator overloading using compile time polymorphismAIM:
To write a C++ programfor operator overloadingusingcompile time polymorphism.
ALGORITHM:
Step 1: Start the program.
Step2: Include necessary header files.
Step 3: Create class box with length, breadth, height.
Step 4: Declare Box1, Box 2, and Box 3 of type Box.
Step 5: Store the volume of a box in volume.
Step 6: Display the result.
Step 7: End the program
PROGRAM:
#include<iostream.h>
#include<conio.h>
class Box
{
double length;
double breadth;
double height;
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
};
int main( )
{
Box Box1;
Box Box2;
Box Box3;
double volume = 0.0;
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
volume = Box1.getVolume();
cout< "Volume of Box1 : " < volume <endl;
volume = Box2.getVolume();
cout< "Volume of Box2 : " < volume <endl;
Box3 = Box1 + Box2;
volume = Box3.getVolume();
cout< "Volume of Box3 : " < volume <endl;
return 0;
}
OUTPUT:
RESULT:
Thus the implementation of operator overloading using compile time polymorphism is executed successfully.
Ex. No. : 3(B) / Function overloading using compile time polymorphismAIM:
To write a C++ programfor function overloading using compile time polymorphism
ALGORITHM:
Step 1: Start the program.
Step2: Include necessary header files.
Step 3: Declare and define add function with parameter.
Step 4: Declare return type to display the output.
Step 5: End the program
PROGRAM:
#include<iostream.h>
#include<conio.h>
void add(int,int);
void add(int,int,int);
float add(float,int);
float add(float,float,int);
void add(int a,int b)
{
cout<a<" + "<b<" = "<a+b;
}
void add(int a,int b,int c)
{
cout<"\n"<a<" + "<b<" + "<c<" = "<a+b+c;
}
float add(float a,int b)
{
return(a+b);
}
float add(float a,float b,int c)
{
return(a+b+c);
}
void main()
{
add(5,10);
add(5,10,15);
float a = 10.5;
int c = 12;
cout<"\n"<a<" + "<c <" = "<add(a,c);
float b = 12.6;
cout<"\n"<a<" + "<b<" + "<c<" = "<add(a,b,c);
}
OUTPUT:
RESULT:
Thus the implementation of function overloading using compile time polymorphism is executed successfully.
Ex. No. : 4(A) / Inheritance using run time polymorphismAIM:
To implement the inheritance and run-time polymorphism using C++.
ALGORITHM:
Step 1: Defining the namespace, class, variable and function.
Step 2: Class bank displays the customer name and account number.
Step 3: Inherit properties of the class bank in newly created class savings.
Step 4: Class savings to perform the deposit and withdraw process.
Step 5: Class current inherits with class savings to display the account information.
Step 6: By creating the object for a class currentall the values are accessed and calling the functions by object variable for performing banking operation.
Step 7: Stop the process.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class bank
{
public:
long int acno;
virtual void display()
{
cout<"Account Number :"<acno;
}
};
class savings : public bank
{
public:
int wdraw, dep, bal;
void saves()
{
int ch;
cout<"Enter the A/c type,1.Deposit 2.Withdraw\n";
cin>ch;
switch(ch)
{
case 1:
cout<"Enter the amount to be deposited = Rs.";
cin>dep;
bal+=dep;
cout<"Your A/c Balance is=Rs."<bal<endl;
break;
case 2:
cout<"Enter the amount to be withdrawn = Rs.";
cin>wdraw;
if(bal<wdraw)
{
cout<"Unavailable Balance. Enter amount less than :" <bal;
}
else
{
bal-=wdraw;
cout<"Your A/c Balance is=Rs."<bal<endl;
}
break;
default:
cout<"Invalid Choice";
break;
}
}
};
class current : public savings
{
public:
void display()
{
cout<"Last amount withdrawn=Rs."<wdraw<endl;
cout<"Last amount deposited=Rs."<dep<endl;
cout<"Your A/c Balance is=Rs."<bal<endl;
}
};
int main()
{
int ch;
current ac;
ac.wdraw=ac.dep=ac.bal=0;
cout<endl<"Enter your A/c no.:";
cin>ac.acno;
cout<endl;
while(1)
{
cout<endl<"Account Number : "<ac.acno<endl;
cout<"Enter the A/c type,1.Savings 2.Current 3.Exit\n";
cin>ch;
switch(ch)
{
case 1:
ac.saves();
break;
case 2:
ac.display();
break;
case 3:
break;
default:
cout<"Invalid Choice";
break;
}
if (ch==3)
break;
}
return(0);
}
OUTPUT:
RESULT:
Thus the implementation of inheritance using run time polymorphism is executed successfully.
Ex. No. : 4(B) / Virtual functionsAIM:
To write a C++ program for virtual functions using run time polymorphism.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class base.
Step 3: Declare and define the virtual function show().
Step 4: Declare and define the function display().
Step 5: Create the derived class from the base class.
Step 6: Declare and define the functions display() and show().
Step 7: Create the base class object and pointer variable.
Step 8: Call the functions display() and show() using the base class object and pointer.
Step 9: Create the derived class object and call the functions display () and show () using the derived class object and pointer.
Step 10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<"\n Base class show:";
}
void display()
{
cout<"\n Base class display:" ;
}
};
classdrive:public base
{
public:
void display()
{
cout<"\n Drive class display:";
}
void show()
{
cout<"\n Drive class show:";
}
};
void main()
{
base obj1;
base *p;
cout<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<"\n\n\t P points to drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}
OUTPUT:
RESULT:
Thus the implementation of virtual functions using run time polymorphism is executed successfully.
Ex. No. : 4(C) / virtual base classesAIM:
To write a C++ programfor virtual base classesusing run time polymorphism.
ALGORITHM:
Step 1: Start the program.
Step2: Declare the class student.
Step 3:Declare and define the functions display () and show ().
Step 4: Declare the base class base.
Step 5: Student base class is inherited as virtual.
Step 6: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<"Enter Roll No:";
cin>rno;
}
voidputnumber()
{
cout<"\n\n\tRoll No:"<rno<"\n";
}
};
class test:virtual public student
{
public:
int part1,part2;
voidgetmarks()
{
cout<"Enter Marks\n";
cout<"Part1:";
cin>part1;
cout<"Part2:";
cin>part2;
}
void putmarks()
{
cout<"\tMarks Obtained\n";
cout<"\n\tPart1:"<part1;
cout<"\n\tPart2:"<part2;
}
};
class sports:public virtual student
{
public:
int score;
void getscore()
{
cout<"Enter Sports Score:";
cin>score;
}
void putscore()
{
cout<"\n\tSports Score is:"<score;
}
};
class result:public test,public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<"\n\tTotal Score:"<total;
}
};
void main()
{
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
OUTPUT:
RESULT:
Thus the implementation of virtual base classes using run time polymorphism is executed successfully.
Ex. No. : 4(D) / TemplatesAIM:
To write a C++ program for templates using run time polymorphism.
ALGORITHM:
Step 1: Defining the namespace, class, variable and function.
Step 2: Declaring the template class T.
Step 3: Declaring the get function for getting the value to bubble sort.
Step 4: Declaring the display function to display the value before bubble sorting.
Step 5: Declaring the sort function for bubble sorting process.
Step 6: The integer, float and character are bubble sorted.
Step 7: Separate object are created for bubble sorting integer, float and character.
Step 8: Stop the process.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
template<class t>
class bubble
{
t a[25];
public:
void get(int);
void sort(int);
void display(int);
};
template<class t>
void bubble <t>::get(int n)
{
int i;
cout<"\nEnter the array elements:";
for(i=0;i<n;i++)
cin>a[i];
}
template<class t>
void bubble <t>::display(int n)
{
int i;
cout<"\nThe sorted array is...\n";
for(i=0;i<n;i++)
cout<a[i]<setw(10);
}
template<class t>
void bubble <t>::sort(int n)
{
int i,j;
t temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }}
int main()
{
bubble<int>obj1;
bubble<float>obj2;
bubble<char>obj3;
int w,n;
do
{
cout<"\nBubble SORT\n";
cout<"\n1.Integer Sort\t2.Float Sort\t3.Character Sort\t4.Exit\n";
cout<"\nEnter Ur choice:";
cin>w;
switch(w)
{
case 1:
cout<"\nBubble Sort on Integer Values...";
cout<"\nEnter the size of array:\n";
cin>n;
obj1.get(n);
obj1.sort(n);
obj1.display(n);
break;
case 2:
cout<"\nBubble Sort on Float Values...";
cout<"\nEnter the size of array:\n";
cin>n;
obj2.get(n);
obj2.sort(n);
obj2.display(n);
break;
case 3:
cout<"\nBubble Sort on Character Values...";
cout<"\nEnter the size of array:\n";
cin>n;
obj3.get(n);
obj3.sort(n);
obj3.display(n);
break;
case 4:
break;
default:
cout<"Invalid Choice\n";
break;
}}while(w!=4);
return(0);
}
OUTPUT:
RESULT:
Thus the implementation of templates using run time polymorphism is executed successfully.
Ex. No. : 5(A) / File Handling - Sequential AccessAIM:
To write a C++ program using sequential access in files Handling.
ALGORITHM:
Step 1: Start the program.
Step2: Include necessary header files.
Step 3: Initialize character array.
Step 4: Open a file in write mode.
Step 5: Write inputted data into the file.
Step 6: Stop the program.
Step 7: Open a file in read mode.
Step 8: Again read the data from the file and display it.
Step 9: Stop the program.
PROGRAM:
#include <fstream.h
#include<iostream.h>
#include<conio.h>
int main ()
{
char data[100];
ofstream outfile;
outfile.open("afile.txt");
cout< "Writing to the file" <endl;
cout< "Enter your name: ";
cin.getline(data, 100);
outfile< data <endl;
cout< "Enter your age: ";
cin> data;
cin.ignore();
outfile< data <endl;
outfile.close();
ifstream infile;
infile.open("afile.txt");
cout< "Reading from the file" <endl;
infile> data;
cout< data <endl;
infile> data;
cout< data <endl;
infile.close();
return 0;
}
OUTPUT:
RESULT:
Thus the implementation of sequential access in file Handling is executed successfully.
Ex. No. : 5(B) / File Handling - Random AccessAIM:
To write a C++ program using random access in files Handling
ALGORITHM:
Step 1: Start the program.
Step2: Include necessary header files.
Step 3: Create new text file test.txt.
Step 4: open the file using fopen.
Step 5: fwrite used to write content from one file to another.
Step 6: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include <string.h>
int main()
{
const char * filename="test.txt";
const char * mytext="Once upon a time there were three bears.";
intbyteswritten=0;
FILE * ft= fopen(filename, "wb") ;
if (ft)
{
fwrite(mytext,sizeof(char),strlen(mytext), ft) ;
fclose(ft ) ;
}
Cout<"len of mytext = %i ",strlen(mytext);
return 0;
}
OUTPUT:
RESULT:
Thus the implementation of random access in file Handling is executed successfully.
Programming using JAVA
Ex. No. : 6 / String Handling in JAVAAIM:
To write a java programforhandling strings
ALGORITHM:
Step 1: Start the program.
Step2: Create new object for class str.
Step 3: Create new string.
Step 4: Apply the string functions in the given string.
Step 5: Stop the program.
PROGRAM:
import java.lang.*;
class str
{
void stringhandlers()
{
String s=new String("ksriet") ;
String n1=s.substring(0,3);
String n2=s.substring(3);
System.out.println(" N1 =" + n1 + "N2=" + n2);
System.out.println("Upper Case" + s.toUpperCase());
System.out.println("Upper Case" + s.toLowerCase());
System.out.println("Replacing String" + s.replace("IET","CT"));
String s1=s.trim();
System.out.println("Before Trim" + s + "trim" + s1 +"sss");
System.out.println(s.charAt(1)) ; }
}
public class stringhandling
{
public static void main(String a[])
{
str s = new str();
s.stringhandlers(); } }
OUTPUT:
RESULT:
Thus the implementation of String Handling in JAVA is executed successfully.
Ex. No. : 7 / Creating user defined packagesAIM:
To write a java programfordeveloping user defined packages
ALGORITHM:
Step 1: Start the program.
Step2: Create package Mypackage with class MyClass
Step 3:Create new object obj for the package