PRACTICAL MANUAL ON C ++

PREFACE

The practical manual on “C ++” has been prepared for B.E. Computer Science & Engineering Students. The “C ++” is increasingly becoming the default choice of the IT industry especially industries involved in software development at system level.

Therefore, for proper development of “C ++” skills among the students this practical manual has been prepared. The manual contains the exercise programs and their solution for easy & quick understanding of the students. The author has gathered material from Books, Journals and Web resources.

We hope that this practical manual will be helpful for students of Computer Science & Engineering for understanding the subject from the point of view of applied aspects

There is always scope for improvement in the manual. We would appreciate to receive valuable suggestions from readers and users for future use.

 

TABLE OF CONTENTS

S.

No.

Laboratory Exercises  

 

1. Write a Program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class.  

 

2. Write a Program using class to process Shopping List for a Departmental Store. The list include details such as the Code No and Price of each item and perform the operations like Adding, Deleting Items to the list and Printing the Total value of a Order.  

 

 

3. Write a Program which creates & uses array of object of a class.( for eg. implementing the list of Managers of a Company having details such as Name, Age, etc..).  

 

4. Write a Program to find Maximum out of Two Numbers using

friend function.

Note: Here one number is a member of one class and the other number is member of some other class.

 

 

5. Write a Program to swap private data members of classes named as class_1, class_2 using friend function.  

 

6. Write a Program to design a class complex to represent complex numbers. The complex class should use an external function (use it as a friend function) to add two complex numbers. The function should return an object of type complex representing the sum of two complex numbers.  

 

 

7. Write a Program using copy constructor to copy data of an object to another object.  

 

8. Write a Program to allocate memory dynamically for an object of a given class using class’s constructor.  

 

9. Write a Program to design a class to represent a matrix. The class should have the functionality to insert and retrieve the elements of the matrix.  

 

10. Write a program to design a class representing complex numbers and having the functionality of performing addition & multiplication of two complex numbers using operator overloading.  

 

11. Write a Program to overload operators like *, <<, >> using friend function. The following overloaded operators should work for a class vector.  

 

12. Write a program for developing a matrix class which can handle integer matrices of different dimensions. Also overload the operator for addition, multiplication & comparison of matrices.  

 

1. Write a Program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the

#include<iostream.h>

#include<conio.h>

class test

{

int code;

static int count;

public:

void setcode(void)

{

code = ++count;

}

void showcode(void)

{

cout<<“object number:”<<code<<“\n”;

}

static void showcount(void)

{

cout<<“count:”<<count<<“\n”;

}

};

int test :: count;

int main()

{

test t1,t2;

t1.setcode();

t2.setcode();

test :: showcount();

test t3; t3.setcode();

test :: showcount();

t1.showcode();

t2.showcode();

t3.showcode();

return 0;

}

2.  Write a Program using class to process Shopping List for a Departmental Store. The list include details such as the Code No and Price of each item and perform the operations like Adding, Deleting Items to the list and Printing the Total value of a Order.

#include<iostream.h>

const m=50;

class ITEMS

{

int itemCode[m];

float itemPrice[m];

int count;

public:

void CNT(void){count=0;}

void getitem(void);

void displaySum(void);

void remove(void);

void displayItems(void);

};

void ITEMS :: getitem(void)

{

cout<<“Enter item code”;

cin>>itemCode[count];

cout<<“Enter Item cost”;

cin>>itemPrice[count];

count++;

}

void ITEMS :: displaySum(void)

{

float sum=0;

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

sum=sum+itemPrice[i];

cout<<“\n Total Value:”<<sum<<“\n”;

}

void ITEMS :: remove(void)

{

int a;

cout<<“Enter Item Code”;

cin>>a;

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

if(itemCode[i] == a)

itemPrice[i]=0;

}

void ITEMS :: displayItems(void)

{

cout<<“\n Code    Price\n”;

 

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

{

cout<<“\n”<<itemCode[i];

cout<<”            “<<itemPrice[i];

}

cout<<“\n”;

}

int main()

{

ITEMS order;

order.CNT();

int x;

do

{

cout<<“\n You can do the following;”

<<“Enter appropriate number\n”;

cout<<“\n1 : Add an Item”;

cout<<“\n2 : Display Total Value”;

cout<<“\n3 : Delete an Item”;

cout<<“\n4 : Display all items”;

cout<<“\n5 : Quit”;

cout<<“\n\n What is your option?”;

cin>>x;

switch(x)

{

case 1 : order.getitem(); break;

case 2 : order.displaySum(); break;

case 3 : order.remove(); break;

case 4 : order.displayItems(); break;

default : cout<<“Error in input”;

}

}while(x!=5);

return 0;

}

3. Write a Program which creates & uses array of object of a ( for eg. implementing the list of Managers of a Company having details such as Name, Age, etc..).

#include<iostream.h>

#include<conio.h>

class employee

{

char name [30];

float age;

public:

void getdata(void);

void putdata(void);

};

void employee  ::  getdata(void)

{

cout<<“Enter Name”;

cin>>name;

cout<<“Enter Age”;

cin>>age;

}

void employee :: putdata(void)

{

cout<<“Name:”<<name<<“\n”;

cout<<“Age:              “<<age<<“\n”;

}

const int size=3;

int main()

{

employee manager[size];

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

{

cout<<“\n Details of manager”<<i+1<<“\n”;

manager[i].getdata();

}

cout<<“\n”;

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

{

cout<<“\n Manager”<<i+1<<“\n”;

manager[i].putdata();

}

return 0;

}

4. Write a Program to find Maximum out of Two Numbers using friend Function. 

Note: Here one number is a member of one class and the other number is member of some other class. 

#include<iostream.h>

#include<conio.h>

class ABC;

class XYZ

{

int x;

public:

void setvalue(int i)

{

x=i;

}

friend void max(XYZ, ABC);

};

class ABC

{

int a;

public:

void setvalue(int i)

{

a=i;

}

friend void max(XYZ, ABC);

};

void max (XYZ m, ABC n)

{

if(m.x>=n.a)

cout<<m.x;

else

}

cout<<n.a;

int main()

{

ABC abc;

abc.setvalue(10);

XYZ xyz;

xyz.setvalue(20);

max(xyz,abc);

return 0;

}

5.  Write a Program to swap private data members of classes named as class_1, class_2 using friend function.

#include<iostream.h>

#include<conio.h>

class class_2;

class class_1

{

int value1;

public:

void indata(int a)

{

value1=a;

}

void display(void)

{

cout<<value1<<“\n”;

}

friend void exchange(class_1 &, class_2 &);

};

class class_2

{

int value2;

public:

void indata(int a)

{

value2=a;

}

void display(void)

{

cout<<value2<<“\n”;

}

friend void exchange(class_1 &, class_2 &);

};

void exchange(class_1 &x, class_2 &y)

{

int temp = x.value1;

x.value1 = y.value2;

y.value2 = temp;

}

int main()

{

class_1 C1;

class_2 C2;

C1.indata(100);

C2.indata(200);

cout<<“Values before exchange”<<“\n”;

C1.display();

C2.display();

exchange(C1, C2);

cout<<“Values after exchange”<<“\n”;

C1.display();

C2.display();

return 0;

}

6. Write a Program to design a class complex to represent complex numbers. The complex class shuold use an external function (use it as a friend function) to add two complex numbers.The function should return an object of type complex representing the sum of two complex numbers.

#include<iostream.h>

#include<conio.h>

class complex

{

float x;

float y;

public:

void input(float real, float img)

{

x=real;

y=img;

}

friend complex sum(complex, complex);

void show(complex);

};

complex sum(complex c1, complex c2)

{

complex c3;

c3.x = c1.x + c2.x;

c3.y = c1.y + c2.y;

return (c3);

}

void complex :: show(complex c)

{

cout<<c.x<<“+j”<<c.y<<“\n”;

}

int main()

{

complex A,B,C;

A.input(3.1, 5.65);

B.input(2.75, 1.2);

C=sum(A,B);

cout<<“A=”;

A.show(A);

cout<<“B=”;

B.show(B);

cout<<“C=”;

C.show(C);

return 0;

}

7. Write a Program using copy constructor to copy data of an object toanother object.

#include<iostream.h>

#include<conio.h>

class code

{

int id;

public:

code() {}

code(int a)

{

id = a;

}

code(code & x)

{

id = x.id;

}

void display(void)

{

cout<<id;

}

};

int main()

{

code A(100);

code B(A);

code C = A;

code D;

D = A;

cout<<“\n id of A:”;

A.display();

cout<<“\n id of B:”;

B.display();

cout<<“\n id of C:”;

C.display();

cout<<“\n id of D:”;

D.display();

return 0;

}

8. Write a Program to allocate memory dynamically for an objects of a given class using class’s 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);

name= new char[length + 1];

strcpy(name, s);

}

void display(void)

{

cout<<name<<“\n”;

}

void join(String &a, String &b);

};

void String :: join (String &a, String &b)

{

length = a.length + b.length;

delete name;

name = new char [length + 1];

strcpy(name,a.name);

strcat(name, b.name);

};

int main()

{

char *first = “Joseph”;

String name1(first), name2(“Louis “), name3(“Lagrange”),s1,s2;

s1.join(name1, name2);

s2.join(s1, name3);

name1.display();

name2.display();

name3.display();

s1.display();

s2.display();

return 0;

}

9. Write a Program to design a class to represent a matrix. The class should have the functionality to insert and retrieve the elements of the matrix.

#include<iostream.h>

class matrix

{

int **p;

int d1,d2;

public:

matrix(int x, int y);

void get_element(int i, int j, int value)

{

p[i][j]=value;

}

int & put_element( int i, int j )

{

return p[ i ] [ j ];

}

};

matrix ::matrix(int x, int y)

{

d1 = x;

d2 = y;

p = new int *[d1];

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

p[i] = new int[d2];

}

int main()

{

int m, n;

cout<<“Enter size of matrix”;

cin>>m>>n;

matrix A(m,n);

cout<<“Enter Matrix Element row by row:”;

int i,j,value;

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

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

{

cin>>value;

A.get_element(i,j,value);

}

cout<<“\n”;

cout<<A.put_element(1,2);

return 0;

}

10. Write a program to design a class representing complex numbers and having the functionality of performing addition & multiplication of two complex numbers using operator overloading.

#include <iostream.h>

class complex

{

private:

float real,

imag;

public:

complex( )

{

}

complex( float r, float i )

{

real = r;

imag = i;

}

void getdata( )

{

float r,

i;

cout << endl << “Enter real and imaginary part “;

cin >> r >> i;

real = r;

imag = i;

}

void setdata( )

{

real = r;

imag = i;

}

void displaydata( )

{

cout << endl << “real = ” << real;

cout<<endl<<Imaginary = “<<imag;

}

complex operator +( complex c )

{

complex t;

t.real = real + c.real;

t.imag = imag + c.imag;

}

complex operator *( complex c )

{

complex t;

t.real = real * c.real – imag * c.imag;

t.imag = real * c.imag + c.real * imag;

return t;

}

}    ;

void main( )

{

complex c1,

c2 ( 1.2, -2.5 ),

c3,

c4;

c1.setdata( 2.0, 2.0 );

c3 = c1 + c2;

c3.displaydata( );

c4.getdata( );

complex c5 ( 2.5, 3.0 ),

c6;

c6 = c4 * c5;

c6.displaydata( );

complex c7;

c7 = c1 + c2 * c3;

c7.displaydata( );

}

11. Write a Program to overload operators like *, <<, >> using friend function. The following overloaded operators should work for a class vector.

#include<iostream.h>

#include<conio.h>

const size = 3;

class vector

{

int v[size];

public:

vector();

vector(int *x);

friend vector operator *(int a, vector b);

friend vector operator *(vector b, int a);

friend istream & operator >>(istream &, vector &);

friend ostream & operator <<(ostream &, vector &);

};

vector :: vector()

{

for(int i=0;

i<size;i++) v[i]=0;

}

vector :: vector(int *x)

{

for(int i=0; i<size; i++) v[i] = x[i];

}

vector operator *(int a, vector b)

{

vector c;

for(int i=0;

i<size; i++) c.v[i] = a * b.v[i];

return c;

}

vector operator *(vector b, int a)

{

vector c;

for(int i=0;

i<size; i++) c.v[i] = b.v[i] * a;

return c;

}

istream & operator >> (istream &din, vector &b)

{

for(int i=0;

i<size; i++) din>>b.v[i];

return ( din ) ;

}

ostream & operator << (ostream &dout, vector &b)

{

dout<<“(“<<b.v [0];

for(int i=1;

i<size; i++) dout<<“,”<<b.v[i];

dout<<“)”;

return(dout);

}

int x[size] = {2,4,6};

int main()

{

vector m;

vector n = x;

cout<<“Enter Elements of vector m”<<“\n”;

cin>>m;

cout<<“\n”;

cout<<“m=”<<m<<“\n”;

vector p,q;

p = 2 * m;

q = n * 2;

cout<<“\n”;

cout<<“p=”<<p<<“\n”;

cout<<“q=”<<q<<“\n”;

return 0;

}

12. Write a program for developing a matrix class which can handle integer matrices of different dimensions. Also overload the operator for addition, multiplication & comparison of matrices.

#include <iostream.h>

#include <iomanip.h> class matrix

{

int maxrow, maxcol;

int * ptr;

public:

matrix( int r, int c )

{

maxrow = r;

maxcol = c;

ptr = new int [r * c];

}

void getmat( )

{

int i,j, mat_off,temp;

cout << endl << “enter elements matrix:” << endl;

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

{

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

{

mat_off = i * maxcol + j;

cin >> ptr[ mat_off ];

}

}

}

void printmat( )

{

int i, j, mat_off;

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

{

cout << endl;

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

{

mat_off = i * maxcol + j;

cout << setw( 3 ) << ptr[ mat_off ];

}

}

}

int delmat( )

{

matrix q ( maxrow – 1, maxcol – 1 );

int       sign = 1, sum = 0, i, j,k,count;

int       newsize,newpos,pos,order;

order = maxrow;

if( order == 1 )

{

return ( ptr[ 0 ] );

}

for( i = 0; i < order; i++, sign *= -1 )

{

for( j = 1; j < order; j++ )

{

for( k = 0, count = 0; k < order;

k++ )

{

if( k == i )

continue;

pos = j * order + k;

newpos = ( j – 1 ) * ( order – 1 ) + count;

q.ptr[ newpos ] = ptr[ pos ];

count++;

}

}

sum = sum + ptr[ i ] * sign * q.delmat( );

}

return ( sum );

}

matrix operator +( matrix b )

{

matrix c ( maxrow, maxcol );

int       i,j,mat_off;

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

{

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

{

mat_off = i * maxcol + j;

c.ptr[ mat_off ] = ptr[ mat_off ] + b.ptr[ mat_off ];

}

}

return ( c );

}

matrix operator *( matrix b )

{

matrix c ( b.maxcol, maxrow );

int     i,j,k,mat_off1, mat_off2, mat_off3;

for( i = 0; i < c.maxrow; i++ )

{

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

{

mat_off3 – i * c.maxcol + j;

c.ptr[ mat_off3 ] = 0;

for( k = 0; k < b.maxrow; k++ )

{

mat_off2 = k * b.maxcol + j;

mat_off1 = i * maxcol + k;

c.ptr[mat_off3]+=ptr[mat_off1]* b.ptr[mat_off2 ];

}

}

}

return ( c );

}

int operator ==( matrix b )

{

int i,j, mat_off;

if( maxrow != b.maxrow

|| maxcol != b.maxcol )

return ( 0 );

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

{

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

{

mat_off = i * maxcol + j;

if( ptr[ mat_off ]

!= b.ptr[ mat_off ]

return ( 0 );

}

}

return ( 1 );

}

}  ;

void main( )

{

int rowa, cola, rowb, colb;

cout << endl << “Enter dimensions of matrix A “;

cin >> rowa >> cola;

matrix a ( rowa, cola );

a.getmat( );

cout << endl << “Enter dimensions of matrix B”;

cin >> rowb >> colb;

matrix b ( rowb, colb );

b.getmat( );

matrix c ( rowa, cola );

c = a + b;

cout << endl << “The sum of two matrics = “;

c.printmat( );

matrix d ( rowa, colb );

d = a * b;

cout << endl << “The product of two matrics = “;

d.printmat( );

cout << endl << “Determinant of matrix a =” << a.delmat( );

if( a == b )

cout << endl << “a & b are equal”;

else

cout << endl << “a & b are not equal”;

}

Leave a Reply

Your email address will not be published. Required fields are marked *