Sei sulla pagina 1di 13

DS & OOPS

Lab Manual
J. Joe Paul
DS & OOPS LAB
By
Expt No: 1 Class Implementation
Date:

Aim: To write a C++ program for class implementation.
Algorithm:
Step 1: Start the program.
Step 2: Create the class.
Step 3: Declare the variable and function.
Step 4: Create the object for class name.
Step 5: Calling the function through an object.
Step 6: Execute the program.
Step 7: Stop the program.
Concept:
Class:
A class is a collection of object of similar type. The class is a way to bind the
data and its associated function together. Class specification has two types.
Class declaration
Class function definition.
Syntax:
class class name
{
private:
variable declaration
function declaration
public:
variable declaration
function declaration };



1
Program:
#include <iostream>

using namespace std;

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}

Output: (On Left Page)

Result:
Thus the C++ program by class implementation was executed and output was
verified.

2
Expt No: 2 Function Overloading
Date:
Aim: To write a C++ program for function overloading.
Algorithm:
Step 1: Start the program.
Step 2: Using main function declare the variable 2.
Step 3: Define int volume function, double volume function, long volume function.
Step 4: Call the above function using main.
Step 5: Execute the program.
Step 6: Stop the program.
Concept:
Class:
It means that same function name is given to different function that program a
variety of different tasks.

Program:
#include <iostream>
using namespace std;

class printData
{
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}

void print(double f) {
cout << "Printing float: " << f << endl;
}

void print(char* c) {
cout << "Printing character: " << c << endl;
}

3
};

int main(void)
{
printData pd;

// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");

return 0;
}
Output: (On Left Page)

Result:

Thus the C++ program by function overloading was executed and output was
verified.






















4
Expt No: 3 Friend Function
Date:

Aim: To write a C++ program for a friend function.
Algorithm:
Step 1: Start the program.
Step 2: Declared as friend not in the scope of the class.
Step 3: Use an object name and dot membership operator.
Step 4: Declaring either in the public or the private part.
Step 5: Execute the program.
Step 6: Stop the program.
Concept:
Class:
A friend function is used for accessing the non-public member of a class.
A class can allow the non-member functions and other classes to access its own
private data. Thus a friend function is an ordinary function or member of
another class.
Syntax:
class sample
{
Calling friend function (argument) };


Program:
#include <iostream>
#include <conio.h>
using namespace std;
class base
{
int val1,val2;
public:
void get()

5
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
int main()
{

base obj;
obj.get();
cout<<"\n Mean value is : "<<mean(obj);
getch();
}

Output: (On Left Page)

Result:

Thus the C++ program for friend function was executed and output was verified.












6
Expt No: 4 Transpose of a Matrix
Date:

Aim: To write a C++ program for a transpose of a matrix.
Algorithm:
Step 1: Start the program.
Step 2: Creating a class name matrix.
Step 3: The functions are defined and declared within the class.
Step 4: Using main functions two objects are created under class data type.
Step 5: Function call is done in the main function.
Step 6: Execute the program.
Step 6: Stop the program.

Program:


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[10][10],m,n,i,j;
cout<<"Enter number of rows: ";
cin>>m;
cout<<"Enter number of coloumns: ";
cin>>n;
//Coding by: Snehil Khanor
//http://WapCPP.blogspot.com
if(m!=n)
{
cout<<"Matrix not square so Transpose not possible :(";
}
else
{

7
cout<<endl<<"Enter elements of matrix: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
}
cout<<endl<<"Displaying Matrix: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl<<endl;
}
cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[j][i]<<" ";
}
cout<<endl<<endl;
}
}
getch();
}

Output: (On Left Page)

Result:

Thus the C++ program for transpose of a matrix was executed and output was
verified.







8
Expt No: 5 Binary Operator Overloading
Date:

Aim: To write a C++ program for a binary operator overloading.
Algorithm:
Step 1: Start the program.
Step 2: Create a class name complex.
Step 3: Functions are declared within the class.
Step 4: They are defined outside the class.
Step 5: In the main function and object is created under class datatype.
Step 6: Function Call is done.
Step 7 and a: Execute the program.
Step 8: Stop the program.
Concept:
The predefined operator can be used for different operations.

Program:

/* Simple example to demonstrate the working of operator overloading*/
#include <iostream>
using namespace std;
class temp
{
private:
int count;
public:
temp():count(5){ }
void operator ++() {
count=count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()

9
{
temp t;
++t; /* operator function void operator ++() is called */
t.Display();
return 0;
}

Output: (On Left Page)

Result:

Thus the C++ program for binary operator overloading was executed and output was
verified.


















10
Expt No: 6 Constructor Overloading
Date:

Aim: To write a C++ program for constructor overloading.
Algorithm:
Step 1: Start the program.
Step 2: Create a class name class.
Step 3: Functions are defined and declared within the class.
Step 4: function call is done.
Step 5: Execute the program.
Step 6: Stop the program.

Program:
#include <iostream>
#include <conio.h>
using namespace std;
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
int main()
{

base obj;

11
obj.get();
cout<<"\n Mean value is : "<<mean(obj);
getch();
}

Output: (On Left Page)

Result:

Thus the C++ program for constructor overloading was executed and output was
verified.



12

Potrebbero piacerti anche