Monday, April 10, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 8- Write a C++ Program to create two classes each class consists of two private variables, an integer and a float variable. Write member functions to get and display them. Write a FRIEND function common to both classes, which takes the object of above two classes as arguments and the integer and float values of both objects separately and display the result.

 

Bharathiar University

Programming in C++ Lab
C++ Program 8
Write a C++ Program to create two classes each class consists of two private variables, an integer and a float variable. Write member functions to get and display them. Write a FRIEND function common to both classes, which takes the object of above two classes as arguments and the integer and float values of both objects separately and display the result.

In this page

  • C++ Source Code for MS Dos Turbo C++

Source Code for MS Dos Turbo C++

#include<iostream.h>
#include<conio.h>
class B;
class A
{
    private:  
int a;
float b;
    public:
void get()
{
    cout<<"\nEnter integer value of class A: ";
    cin>>a;
    cout<<"Enter float value of class A: ";
    cin>>b;

}
void display(){
    cout<<"\nClass A";
    cout<<"\n=======";
    cout<<"\nInteger value: "<<a;
    cout<<"\nFloat value: "<<b;
}
friend void sum(A ob1,B ob2);
};
class B
{
    private:
int x;
float y;
    public:
void get()
{
    cout<<"\nEnter integer value of class B: ";
    cin>>x;
    cout<<"Enter float value of class B: ";
    cin>>y;

}
void display(){
    cout<<"\nClass B";
    cout<<"\n========";
    cout<<"\nInteger value: "<<x;
    cout<<"\nFloat value: "<<y;
}
friend void sum(A ob1,B ob2);
};

void sum(A ob1,B ob2)
{
    cout<<"\n\nFriend Function";
    cout<<"\n===============";
    cout<<"\nSum of Integer values class A and class B: "<<ob1.a+ob2.x;
    cout<<"\nSum of Float values of class A and class B: "<<ob1.b+ob2.y;
}
int main()
{
    A c1;
    B c2;
    clrscr();
    c1.get();
    c2.get();
    c1.display();
    c2.display();
    sum(c1,c2);
    getch();
    return 0;
}

OUTPUT


BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 7- Write a C++ program to create a class SHAPE which consists of two virtual functions calculate_area() and calculate_perimeter() to calculate area and perimeter of various figures. Derive three classes SQUARE, RECTANGLE, TRIANGLE from class Shape and Calculate Area and Perimeter of each class separately and display the result.

 

Bharathiar University

Programming in C++ Lab
C++ Program 7
Write a C++ program to create a class SHAPE which consists of two virtual functions calculate_area() and calculate_perimeter() to calculate area and perimeter of various figures. Derive three classes SQUARE, RECTANGLE, TRIANGLE from class Shape and Calculate Area and Perimeter of each class separately and display the result.
In this page

  • C++ Source Code for MS Dos Turbo C++

Source Code for MS Dos Turbo C++
#include <iostream.h>
#include<conio.h>

class Shape
{
  public:
    virtual void calculate_area()
    {
        cout<<"Area: Virtual Function";
    }
    virtual void calculate_perimeter()
    {
        cout<<"Perimeter: Virtual Function";
    }
    
};
class Square : public Shape
{
  public:
  float side;
  Square(float x)
  {
    side=x;
  }
    void calculate_area()
    {
        cout<<"Area of Square with side="<<side<<": ";
        cout<<side*side;
    }
     void calculate_perimeter()
    {
        cout<<"\nPerimeter of Square with side="<<side<<": ";
        cout<<4*side;
    }

};
class Rectangle: public Shape
{
 public:
  float length,breadth;
  Rectangle(float x,float y)
  {
    length=x;
    breadth=y;
  }
    void calculate_area()
    {
        cout<<"Area of Rectangle with length="<<length<<"and breadth="<<breadth<<": ";
        cout<<length*breadth;
    }
     void calculate_perimeter()
    {
        cout<<"\nPerimeter of with length="<<length<<"and breadth="<<breadth<<": ";
        cout<<2*(length+breadth);
    }
};

class Triangle: public Shape
{
public:
  float base, height,side1,side2;
  Triangle(float s1,float s2,float b,float h)
  {
    side1=s1;
    side2=s2;
    base=b;
    height=h;
  }
    void calculate_area()
    {
        cout<<"Area of Triangle with";
        cout<<" side1="<<side1<<" side2="<<side2;
        cout<<" base="<<base<<" height="<<height<<": ";
        cout<<(base*height)/2;
    }
     void calculate_perimeter()
    {
        cout<<"\nPerimeter of Triangle with";
        cout<<" side1="<<side1<<" side2="<<side2;
        cout<<" base="<<base<<" height="<<height<<": ";
        cout<<side1+side2+base+height;
    }
};



int main()
{
   float side,length,breadth,side1,side2,base,height;
   Shape *s;
   clrscr();

   cout<<"\nArea and Perimeter of Shapes using Virtual Funtions"<<endl;
    cout<<"===================================================="<<endl;
   cout<<"Square"<<endl;
   cout<<"Enter side of a square:";
   cin>>side;
   Square sq(side);
   s=&sq;
   s->calculate_area();
   s->calculate_perimeter();

   cout<<"\n\nRectangle"<<endl;
   cout<<"Enter length of rectangle:";
   cin>>length;
   cout<<"Enter breadth of rectangle:";
   cin>>breadth;
   Rectangle r(length,breadth);
   s=&r;
   s->calculate_area();
   s->calculate_perimeter();
        
   cout<<"\n\nTriangle"<<endl;
   cout<<"Enter side1 of Triangle:";
   cin>>side1;
   cout<<"Enter side2 of Triangle:";
   cin>>side2;
   cout<<"Enter base of Triangle:";
   cin>>base;
   cout<<"Enter height of Triangle:";
   cin>>height;
   Triangle t(side1,side2,base,height);
   s=&t;
   s->calculate_area();
   s->calculate_perimeter();  
getch();
 return 0;
}

OUTPUT:


Sunday, April 2, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 9-Write a C++ program using Function Overloading to read two matrices of different data types such as integers and floating point numbers. Find out the sum of the above two matrices separately and display the sum of these arrays individually

 

Bharathiar University

Programming in C++ Lab
C++ Program 9
Write a C++ program using Function Overloading to read two matrices of different data types such as integers and floating point numbers. Find out the sum of the above two matrices separately and display the sum of these arrays individually

In this page

  • C++ Source Code for MS Dos Turbo C++
  • C++ Source Code for Visual studio code
Source code for Turbo C++
#include<iostream.h>
#include<conio.h>

class Matrix
{
   public:
int a[3][3],b[3][3];
float x[3][3],y[3][3];
void readint();
void readfloat();
void sum(int a[3][3],int b[3][3]);
void sum(float x[3][3],float y[3][3]);
};
void Matrix::readint()
{
int i,j;
cout<<"\nEnter integer Matrix 1 : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter integer Matrix 2 : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>b[i][j];
}
}
}
void Matrix::readfloat()
{
int i,j;
cout<<"\nEnter float Matrix 1 :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>x[i][j];
}
}
cout<<"\n Enter float Matrix 2 :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>y[i][j];
}
}
}
void Matrix::sum(int a[3][3],int b[3][3])
{
cout<<"\n Addition of Integer Matrices : \n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]+b[i][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::sum(float x[3][3],float y[3][3])
{
cout<<"\n Addition of Float Matrices : \n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<x[i][j]+y[i][j]<<"\t";
}
cout<<"\n";
}
}

int main()
{
Matrix m;
clrscr();
m.readint();
m.readfloat();
m.sum(m.a,m.b);
m.sum(m.x,m.y);
getch();
return 0;
}

OUTPUT:
Enter integer Matrix 1 : 1 2 3
1 2 3
1 2 3

Enter integer Matrix 2 : 1 1 1
1 1 1
1 1 1

Enter float Matrix 1 :2.3 2.1 3.2
2.3 5.4 4.3
2.3 4.3 5.4

 Enter float Matrix 2 :1.1 2.2 3.3
2.1 1.1 4.2
1.1 1.5 1.2

 Addition of Integer Matrices :
 2      3       4
 2      3       4
 2      3       4

 Addition of Float Matrices :
 3.4    4.3     6.5
 4.4    6.5     8.5
 3.4    5.8     6.6



Source code for Visual studio code
#include<iostream>
#include<conio.h>
using namespace std;
class Matrix
{
   public:
int a[3][3],b[3][3];
float x[3][3],y[3][3];
void readint();
void readfloat();
void sum(int a[3][3],int b[3][3]);
void sum(float x[3][3],float y[3][3]);
};
void Matrix::readint()
{
int i,j;
cout<<"\nEnter integer Matrix 1 : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter integer Matrix 2 : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>b[i][j];
}
}
}
void Matrix::readfloat()
{
int i,j;
cout<<"\nEnter float Matrix 1 :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>x[i][j];
}
}
cout<<"\n Enter float Matrix 2 :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>y[i][j];
}
}
}
void Matrix::sum(int a[3][3],int b[3][3])
{
cout<<"\n Addition of Integer Matrices : \n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]+b[i][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::sum(float x[3][3],float y[3][3])
{
cout<<"\n Addition of Float Matrices : \n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<x[i][j]+y[i][j]<<"\t";
}
cout<<"\n";
}
}

int main()
{
Matrix m;
m.readint();
m.readfloat();
m.sum(m.a,m.b);
m.sum(m.x,m.y);
return 0;
}
Output
Enter integer Matrix 1 : 1 2 3
1 2 3
1 2 3

Enter integer Matrix 2 : 1 1 1
1 1 1
1 1 1

Enter float Matrix 1 :2.3 2.1 3.2
2.3 5.4 4.3
2.3 4.3 5.4

 Enter float Matrix 2 :1.1 2.2 3.3
2.1 1.1 4.2
1.1 1.5 1.2

 Addition of Integer Matrices :
 2      3       4
 2      3       4
 2      3       4

 Addition of Float Matrices :
 3.4    4.3     6.5
 4.4    6.5     8.5
 3.4    5.8     6.6

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...