EXPT NO: 01 CONSTRUCTORS & DESTRUCTORS, COPY CONSTRUCTOR. Aim: To implement the concept of Constructor, destructor and copy constructor using C++ language Algorithm: Step 1: Start the program Step 2: Create class with two constructors. Step 3: Create the objects for the class with arguments. Step 4: Invoke the member function using objects. Step 5: Display the result. Step 6: Stop the program PROGRAM: // 1. Constructor, Destructor, copy constructor #include<iostream.h> #include<string.h> #include<conio.h> class string { char*name; int length; public: string() { length=0; name=new char[length+1]; } string(char*s) { length=strlen(s); strcpy(name,s); } void display(void)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Aim:To implement the concept of Constructor, destructor and copy constructor using C++
language
Algorithm:
Step 1: Start the programStep 2: Create class with two constructors.Step 3: Create the objects for the class with arguments.Step 4: Invoke the member function using objects.Step 5: Display the result.Step 6: Stop the program
Dynamic memory Allocation, Constructor & Destructor joseph louis lagrange josephlouis josephlouislagrange Null pointer assignment
Result: Thus the C++ program for Constructor, destructor, and copy constructor was verified and executed successfully.
EXPT NO: 02 FRIEND FUNCTION & FRIEND CLASS
Aim:To implement the concept of Friend Function & Friend Class using C++ language
Algorithm:
Step 1: Start the programStep 2: Create friend function and friend class.Step 3: Create the objects for the classStep 4: Invoke the Friend function using objects.Step 5: Display the result.Step 6: Stop the program
#include<iostream.h>
#include<string.h>
//class
class friends
{
//access specifies
private:
// initialization of member variables
int mVar1, mVar2;
//access specifier
public:
// member function
void test()
{
// assigning value to member variables
cout<<"Enter the first value :";
cin>>mVar1;
cout<<"Enter the second value :";
cin>>mVar2;
}
// Declare the two functions friends.
friend int addition(friends input);
friend int subtraction(friends input);
};
// Function one
int addition(friends input)
{
// addition operation performed
return int(input.mVar1 + input.mVar2);
}
// Function two
int subtraction(friends input)
{
// subtraction operation performed
return int(input.mVar2- input.mVar1);
}
// main function
int main( )
{
// object created
friends ofriend;
//accessing member function using the object
ofriend.test();
//print the output of addition
cout << "The addition value is:"<<addition(ofriend) << endl;
// print the output of subtraction
cout << "The subtraction value is:"<<subtraction(ofriend) << endl;
}
Output:
b) Program for friend class#include <iostream>using namespace std;class Square;class Rectangle { int width, height;
public: int area () { return ( width * height ); }
void convert ( Square a ); };
class Square {
friend class Rectangle; private: int side; public: Square ( int a ) : side ( a ) {} };
int main (){ Rectangle rect; Square sqr ( 4 ); rect.convert ( sqr ); cout << rect.area(); return 0;}
Output
Result
Thus the C++ program for Friend function and friend class was verified and executed successfully.
EX. NO: 03 INHERITANCE
Aim:To implement the concept of inheritance using C+ + language.
Algorithm:
Step 1: Start the program.Step 2: Create a class student with two function get_number( )and put_number( ).Step 3: Define the member functions.Step 4: Create a class test inherited from class student which contain two member function get_mark( )and put_mark( ).Step 5: Create a class result inherited from class test which contain a member function display( ).Step 6: Create a object with result class and invoke the function.Step 7: Display the results.Step 8: Stop the program.
Multiple and Multilevel Inheritance roll_number:1234 marks obtained: part1=27.5 part2=33 sports wt:6 total score:66.5
Result: Thus the C++ program for multiple and multilevel inheritance was verified and executed successfully.
EX. NO.: 04 POLYMORPHISM & FUNCTION OVERLOADING.
Aim:To implement the concept of polymorphism, function overloading using C++
programming.
Algorithm:
Step 1: Start the programStep 2: Declare three methods with different parameters with same name.Step 3: Define three methods separately.Step 4: Call the methods by giving values at run time.Step 5: Return the result.Step 6: Stop the program
PROGRAM:
// 1. Function Overloading#include<iostream.h>#include<conio.h>int volume(int);double volume(double,int);long volume(long,int,int);int main() { clrscr(); cout<<"\n\t Function Overloading"; cout<<"\n Volume of a square ="<<volume(10); cout<<"\n Volume of a circle ="<<volume(2.5,8); cout<<"\n Volume of a rectangle ="<<volume(100l,75,15); getch(); return 0; }int volume(int s) { return (s*s*s); }double volume(double r,int h) { return(3.14519*r*r*h);
Function OverloadingVolume of a square =1000Volume of a circle =157.2595Volume of a rectangle =112500
Result: Thus the C++ program for function overloading was verified and executed successfully.
EXPT NO: 05 VIRTUAL FUNCTIONS
Aim:To implement the concept Virtual function using C++ programming.
Algorithm:
Step 1: Start the programStep 2: Declare two methods.Step 3: Define one method as VirtualStep 4: Call the methods by giving values at run time.Step 5: Return the result.Step 6: Stop the program
PROGRAM:
#include <iostream>using namespace std;// abstract base classclass Shape { protected: // attribute section int mWidth; int mHeight; int mResult; public: // behavior section void setVar(int Width,int Height) { mWidth = Width; mHeight= Height; } virtual void area() { // virtual function cout<< "shape drawn"; }
int getresult() { return mResult; } };
// add class inherits from base classclass rectangle: public Shape {
int main(){ int Length,Breath; //pointer variable declaration of type base class Shape* oShape; //create object1 for addition process rectangle oRectangle; //create object2 for subtraction process triangle oTriangle; cout << "\nEnter the width and height: ";
while (cin >> Length >> Breath) { oShape = &oRectangle; oShape->setVar( Length , Breath ); //area of rectangle process, even though call is on pointer to base(shape)! oShape->area(); cout << "\nArea of rectangle = " << oShape->getresult(); oShape = &oTriangle; oShape->setVar( Length , Breath ); //triangle process, even though call is on pointer to base! oShape->area(); cout << "\nArea of triangle = " << oShape->getresult() << endl;
} return 0;}
Output
Result: Thus the C++ program for operator overloading was verified and executed successfully.
EXPT NO: 06 OVERLOADING UNARY AND BINARY OPERATOR USING MEMBER FUNCTION AND NON-MEMBER FUNCTION
Aim:
To implement Operator Overloading using member function and non member function in C++
Algorithm:
Step 1: Start the program.Step 2: Create a class with operator function, other function and variable.Step 3: Define the member functions.Step 4: Create the objects for the class.Step 5: Invoke the member function using object.Step 6: Display the output.Step 7: Stop the program.
Result: Thus the C++ program for operator overloading was verified and executed successfully.
EXPT NO: 07 CLASS AND FUNCTION TEMPLATES
a) Class Template
Aim:To write a C++ program for creating class template.
Algorithm:
Step 1: Start the program Step 2: Create a template vector using template <class T>.Step 3: Define the template.Step 4: Create the object using template vector.Step 5: Do the operations.Step 6: Display the result.Step 7: Stop the program
PROGRAM:
// Class Template#include<iostream.h>#include<conio.h>const size=3;template<class T>class vector { T*V; public: vector() { V=new T[size]; for(int i=0;i<size;i++) V[i]=0; } vector(T*a) { for(int i=0;i<size;i++) V[i]=a[i]; } T operator*(vector&y) { T sum=0; for(int i=0;i<size;i++)
Aim:To write a C++ program for Exception handling with multiple catch statements.
Algorithm:
Step 1: Start the programStep 2: Cerate a function test with conditions in the try block.Step 3: Catch the values which is troughed from the try block.Step 4: Display the messages from the block which is catched.Step 5: Stop the program.
PROGRAM:
#include<iostream.h>#include<conio.h>void main(){ int a,b,c; float d; clrscr(); cout<<"Enter the value of a:"; cin>>a; cout<<"Enter the value of b:"; cin>>b; cout<<"Enter the value of c:"; cin>>c; try { if((a-b)!=0) { d=c/(a-b); cout<<"Result is:"<<d; } else { throw(a-b); } }
catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; } getch();}
OUTPUT:
Enter the value of a: 5Enter the value of b: 4Enter the value of a: 2Result is: 2
Enter the value of a: 5Enter the value of b: 5Enter the value of a: 2Answer is infinite because a-b is: 0
Result: Thus the C++ program for exception handling with multiple catch statements was
verified and executed successfully.
EXPT NO: 09 STL CONCEPTS
Aim:To implement concept STL using C++ program.
Algorithm:
Step 1: Start the program.Step 2: Include the standard template library of list in the header file.Step 3: Using the list STL display the list values, add the element in the list, remove the element from the list, merge two lists, sort and reverse the listStep 4: Display values after each operation.Step 5: Stop the program.
SS.push_back("The number is 10"); SS.push_back("The number is 20"); SS.push_back("The number is 30"); cout << "Loop by index:" << endl; int ii; for(ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; }
Loop by index:The number is 10The number is 20The number is 30
Constant Iterator:The number is 10The number is 20The number is 30
Reverse Iterator:The number is 30The number is 20The number is 10
Sample Output:3The number is 30The number is 10
Result: Thus the C++ program for using STL was verified and executed successfully.
EXPT NO: 10 FILESTREAM CONCEPTS
Aim:To concept of file stream concept using C++
Algorithm:
Step 1: Start the program.Step 2: Include the fstream in the header file.Step 3: perform all function in file stream Step 4: Display values after each operation.Step 5: Stop the program.
PROGRAM:
a) Program to write a text in the file#include<iostream>
#include<fstream>
using namespace std;
int main()
{
// Create object to display output
ofstream oFileoutput;
// Open the text file in write mode
oFileoutput.open ( "out.txt" );
//String declaration
char char_string[300] = "Time is a great teacher, but unfortunately it kills all its students.
Berlioz";
//Display the string
oFileoutput << char_string;
//Close the text file in write mode
oFileoutput.close();
return 0;
}
Output:
b) Program to read data from text file and display it#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
// Create object to get input values
ifstream oFileinput;
// Open the text file in read mode
oFileinput.open ( "out.txt" );
// Character declaration
char char_text;
/* Read the character until the file reach end of file*/
while ( !oFileinput.eof() )
{
oFileinput.get ( char_text );
//Display the text
cout << char_text;
}
//Close the text file opened in read mode
oFileinput.close();
return 0;
}
Output:
c) Program to count number of characters from “out.txt”.#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
// Create object to get input values
ifstream oFileinput;
// Open the text file in read mode
oFileinput.open ( "out.txt" );
// Character declaration
char char_text;
//Integer variable declaration
int int_count = 0;
/* Read the character until the file reach end of file*/
while ( !oFileinput.eof() )
{
// Read characters from the text file
oFileinput.get ( char_text );
// Increment the count values
int_count++;
}
// Display number of characters in text file
cout << "Number of characters in file is " << int_count;
//Close the text file opened in read mode
oFileinput.close();
return 0;
}
Output:
d) Program to count number of words from “out.txt”#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
// Create object to read input values
ifstream oFileinput;
// Open the text file in read mode
oFileinput.open ( "out.txt" );
// Character declaration
char char_word[30];
//Integer variable declaration
int int_count = 0;
/* Read the word until the file reach end of file*/
while ( !oFileinput.eof() )
{
//Read the word from the text file
oFileinput >> char_word;
// Increment the count values
int_count++;
}
// Display number of words in text file
cout << "Number of words in file is " << int_count;
//Close the text file opened in read mode
oFileinput.close();
return 0;
}
Output:
e) Program to count number of lines in the file#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
int main()
{
// Create object to read input values
ifstream oFileinput;
// Open the text file in read mode
oFileinput.open ( "out.txt" );
// Character declaration
char char_word[30];
// Integer variable declaration
int int_count = 0;
/* Read the word until the file reach end of file*/
while ( !oFileinput.eof() )
{
// Read the word from the text file
oFileinput >> char_word;
// Increment the count values
int_count++;
}
// Display number of words in text file
cout << "Number of words in file is " << int_count;
// Close the text file opened in read mode
oFileinput.close();
return 0;
}
Output
f) Program to copy contents of one file to another file.#include<iostream>#include<fstream>#include<conio.h>using namespace std;int main(){
// Create object to read input values
ifstream oFileinput;
// Open the text file in read mode
oFileinput.open ( "out.txt" );
// Create object to display output values
ofstream oFileoutput;
// Open the text file in write mode
oFileoutput.open ( "sample.txt" );
// Character declaration
char char_text;
/* Read the character until the file reach end of file*/