Monday, July 22, 2024

Program 6 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 6 Design a 3x3 matrix class and demonstrate the following: a. Addition and multiplication of two matrices using operator overloading b. Maintaining a count of the number of matrix object created

 


BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 6

Design a 3x3 matrix class and demonstrate the following: 
a. Addition and multiplication of two matrices using operator overloading 
b. Maintaining a count of the number of matrix object created.


SOURCE CODE:

#include <iostream>
using namespace std;

class Matrix3x3 {
private:
    int data[3][3];
    static int objectCount; // Static variable to count the number of objects

public:
    // Constructor to initialize the matrix
    Matrix3x3(int init[3][3]) {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                data[i][j] = init[i][j];
            }
        }
        objectCount++;
    }

    // Default constructor
    Matrix3x3() {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                data[i][j] = 0;
            }
        }
        objectCount++;
    }

    // Destructor
    ~Matrix3x3() {
        objectCount--;
    }

    // Static function to get the number of objects
    static int getObjectCount() {
        return objectCount;
    }

    // Operator overloading for matrix addition
    Matrix3x3 operator+(const Matrix3x3& other) const {
        Matrix3x3 result;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                result.data[i][j] = data[i][j] + other.data[i][j];
            }
        }
        return result;
    }

    // Operator overloading for matrix multiplication
    Matrix3x3 operator*(const Matrix3x3& other) const {
        Matrix3x3 result;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                result.data[i][j] = 0;
                for (int k = 0; k < 3; ++k) {
                    result.data[i][j] += data[i][k] * other.data[k][j];
                }
            }
        }
        return result;
    }
// Function to read matrix from user input
    void readFromUser() {
        cout << "Enter the elements of the 3x3 matrix (row-wise):" << endl;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                cin >> data[i][j];
            }
        }
    }

    // Function to print the matrix
    void print() const {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                cout << data[i][j] << " ";
            }
            cout << endl;
        }
    }
};

// Initialize static member
int Matrix3x3::objectCount = 0;
int main() {
    Matrix3x3 matrix1, matrix2;
    
    cout << "Enter matrix 1:" << endl;
    matrix1.readFromUser();

    cout << "Enter matrix 2:" << endl;
    matrix2.readFromUser();

    cout << "Matrix 1:" << endl;
    matrix1.print();

    cout << "Matrix 2:" << endl;
    matrix2.print();

    Matrix3x3 matrixSum = matrix1 + matrix2;
    cout << "Sum of Matrix 1 and Matrix 2:" << endl;
    matrixSum.print();

    Matrix3x3 matrixProduct = matrix1 * matrix2;
    cout << "Product of Matrix 1 and Matrix 2:" << endl;
    matrixProduct.print();

    cout << "Number of Matrix3x3 objects created: " << Matrix3x3::getObjectCount() << endl;

    return 0;
}


OUTPUT:

Enter matrix 1:
Enter the elements of the 3x3 matrix (row-wise):
1 2  3
4 5 6
3 6 7
Enter matrix 2:
Enter the elements of the 3x3 matrix (row-wise):
3 3 3
2 2 2
1 1 1
Matrix 1:
1 2 3
4 5 6
3 6 7
Matrix 2:
3 3 3
2 2 2
1 1 1
Sum of Matrix 1 and Matrix 2:
4 5 6
6 7 8
4 7 8
Product of Matrix 1 and Matrix 2:
10 10 10
28 28 28
28 28 28
Number of Matrix3x3 objects created: 4


Explanation

  1. Matrix Class:

    • The readFromUser method is added to read matrix elements from the user.
    • The print method displays the matrix elements.
    • Other class members and methods remain unchanged.
  2. Main Function:

    • Two Matrix3x3 objects (matrix1 and matrix2) are created using the default constructor.
    • The readFromUser method is called to read the elements of each matrix from the user.
    • The matrices are displayed using the print method.
    • Addition and multiplication of the matrices are demonstrated using the overloaded operators.
    • The count of matrix objects is displayed using the static member function getObjectCount.

Program 5 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 5 Design a class representing time in hh:mm:ss. Write functions to a. Set and show the time b. Find the difference between two time objects c. Adding a given duration to a time d. Conversion of the time object to seconds

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 5

Design a class representing time in hh:mm:ss. Write functions to a. Set and show the time b. Find the difference between two time objects c. Adding a given duration to a time d. Conversion of the time object to seconds
 
SOURCE CODE:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

class Time {
private:
    int hours;
    int minutes;
    int seconds;

public:
    // Constructor to initialize time
    Time(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}

    // Function to set the time
    void setTime(int h, int m, int s) {
        hours = h;
        minutes = m;
        seconds = s;
    }

    // Function to show the time
    void showTime() const {
        cout << setw(2) << setfill('0') << hours << ":"
                  << setw(2) << setfill('0') << minutes << ":"
                  << setw(2) << setfill('0') << seconds << endl;
    }

    // Function to find the difference between two time objects
    Time difference(const Time &t) const {
        int totalSec1 = hours * 3600 + minutes * 60 + seconds;
        int totalSec2 = t.hours * 3600 + t.minutes * 60 + t.seconds;
        int diff = abs(totalSec1 - totalSec2);

        int h = diff / 3600;
        diff %= 3600;
        int m = diff / 60;
        int s = diff % 60;

        return Time(h, m, s);
    }

    // Function to add a given duration to a time
    void addDuration(int h, int m, int s) {
        int totalSec = hours * 3600 + minutes * 60 + seconds + h * 3600 + m * 60 + s;
        hours = (totalSec / 3600) % 24;
        totalSec %= 3600;
        minutes = (totalSec / 60) % 60;
        seconds = totalSec % 60;
    }

    // Function to convert the time object to seconds
    int toSeconds() const {
        return hours * 3600 + minutes * 60 + seconds;
    }
};

int main() {
    Time t1(2, 45, 30);
    Time t2(5, 50, 15);
    int h,m,s;

    cout << "Time 1: ";
    t1.showTime();

    cout << "Time 2: ";
    t2.showTime();

    Time diff = t1.difference(t2);
    cout << "Difference: ";
    diff.showTime();
    
    cout<<"Enter Hours, minutes and seconds to add to time 1:";
    cin>>h>>m>>s;
    t1.addDuration(h,m,s);
    cout << "Time 1 after adding given duration: ";
    t1.showTime();

    int seconds = t1.toSeconds();
    cout << "Time 1 in seconds: " << seconds << endl;

    return 0;
}


OUTPUT:

Time 1: 02:45:30
Time 2: 05:50:15
Difference: 03:04:45
Enter Hours, minutes and seconds to add to time 1:2 60 30
Time 1 after adding given duration: 05:46:00
Time 1 in seconds: 20760


Explanation:

  1. Constructor: Initializes the time with given hours, minutes, and seconds.
  2. setTime: Sets the time.
  3. showTime: Displays the time in hh:mm
    format.
  4. difference: Calculates the absolute difference between two time objects and returns the result as a new Time object.
  5. addDuration: Adds a given duration to the time object and adjusts for overflow in hours, minutes, and seconds.
  6. toSeconds: Converts the time to the total number of seconds since 00:00:00.


Program 4 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 4 Design and implement a class to represent a Solid object. a. Apart from data members to represent dimensions, use a data member to specify the type of solid. b. Use functions to calculate volume and surface area for different solids.

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 4

Design and implement a class to represent a Solid object. 

a. Apart from data members to represent dimensions, use a data member to specify the type of solid.
b. Use functions to calculate volume and surface area for different solids. 

SOURCE CODE:

#include <iostream>
#include <string>
using namespace std;

class Solid {
protected:
    string type;

public:
    Solid(const string& type) : type(type) {}

    virtual ~Solid() {}

    string getType() const {
        return type;
    }

    virtual double volume() const = 0;
    virtual double surfaceArea() const = 0;
};
class Cube : public Solid {
private:
    double side;

public:
    Cube(double side) : Solid("Cube"), side(side) {}

    double volume() const override {
        return side * side * side;
    }

    double surfaceArea() const override {
        return 6 * side * side;
    }
};
class Sphere : public Solid {
private:
    double radius;

public:
    Sphere(double radius) : Solid("Sphere"), radius(radius) {}

    double volume() const override {
        return (4.0 / 3.0) * 3.14159265358979323846 * radius * radius * radius;
    }

    double surfaceArea() const override {
        return 4 * 3.14159265358979323846 * radius * radius;
    }
};
class Cylinder : public Solid {
private:
    double radius;
    double height;

public:
    Cylinder(double radius, double height) : Solid("Cylinder"), radius(radius), height(height) {}

    double volume() const override {
        return 3.14159265358979323846 * radius * radius * height;
    }

    double surfaceArea() const override {
        return 2 * 3.14159265358979323846 * radius * (radius + height);
    }
};
int main() {
    Cube cube(3);
    Sphere sphere(2);
    Cylinder cylinder(2, 5);

    Solid* solids[] = { &cube, &sphere, &cylinder };

    for (Solid* solid : solids) {
        cout << "Type: " << solid->getType() << endl;
        cout << "Volume: " << solid->volume() << endl;
        cout << "Surface Area: " << solid->surfaceArea() << endl;
        cout << std::endl;
    }

    return 0;
}

To design and implement a class to represent a Solid object in C++, we can use inheritance and polymorphism to handle different types of solids. Here, we'll create a base class Solid and derive specific classes for different types of solids, such as Cube, Sphere, and Cylinder. Each derived class will implement its own methods to calculate volume and surface area.

Here's an example of how this can be done:

Step-by-Step Implementation

  1. Base Class Solid:

    • Contains a data member to specify the type of solid.
    • Declares virtual functions to calculate volume and surface area.
  2. Derived Classes:

    • Implement specific solids like Cube, Sphere, and Cylinder.
    • Implement the methods to calculate volume and surface area.

Explanation

  1. Base Class Solid:

    • The Solid class has a protected data member type and a constructor to initialize it.
    • The class declares pure virtual functions volume() and surfaceArea(), making Solid an abstract class.
  2. Derived Classes (Cube, Sphere, Cylinder):

    • Each derived class initializes the base class with its type.
    • They override the volume() and surfaceArea() methods to provide specific implementations for each solid.
  3. Main Function:

    • Demonstrates how to create instances of different solids and call their methods to calculate volume and surface area.


OUTPUT:

Type: Cube

Volume: 27

Surface Area: 54


Type: Sphere

Volume: 33.5103

Surface Area: 50.2655


Type: Cylinder

Volume: 62.8319

Surface Area: 87.9646


Program 3 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 3Design and implement a class that represents a Harmonic Progression (HP). Implement functions to do the following: a. Generate the HP up to a specified number of terms b. Calculate the sum of the HP to n terms and to infinity c. Generate the nth term of the HP d. Generate the corresponding Arithmetic Progression. (Design and implement a class that encapsulates an AP, and allow the HP class to use its facilities by implementing friend functions.)

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 3

Design and implement a class that represents a Harmonic Progression (HP). Implement functions to do the following: a. Generate the HP up to a specified number of terms b. Calculate the sum of the HP to n terms and to infinity c. Generate the nth term of the HP d. Generate the corresponding Arithmetic Progression. (Design and implement a class that encapsulates an AP, and allow the HP class to use its facilities by implementing friend functions.)

SOURCE CODE:


#include <iostream>
#include <vector>
#include <limits>
#include <cmath> // For log and pow functions
using namespace std;

class ArithmeticProgression {
private:
    double a; // First term
    double d; // Common difference

public:
    ArithmeticProgression(double first_term, double common_difference)
        : a(first_term), d(common_difference) {}

    double nth_term(int n) const {
        return a + (n - 1) * d;
    }

    double sum_to_n_terms(int n) const {
        return n * (2 * a + (n - 1) * d) / 2;
    }

    vector<double> generate_terms(int n) const {
        vector<double> terms;
        for (int i = 1; i <= n; ++i) {
            terms.push_back(nth_term(i));
        }
        return terms;
    }

    friend class HarmonicProgression;
};



class HarmonicProgression {
private:
    ArithmeticProgression ap;

public:
    HarmonicProgression(double first_term, double common_difference)
        : ap(first_term, common_difference) {}

    double nth_term(int n) const {
        return 1.0 / ap.nth_term(n);
    }

    vector<double> generate_terms(int n) const {
        vector<double> terms;
        for (int i = 1; i <= n; ++i) {
            terms.push_back(nth_term(i));
        }
        return terms;
    }

    double sum_to_n_terms(int n) const {
        double sum = 0;
        for (int i = 1; i <= n; ++i) {
            sum += nth_term(i);
        }
        return sum;
    }

    double sum_to_infinity() const {
        if (ap.d == 0) {
            return numeric_limits<double>::infinity();
        }
        return numeric_limits<double>::infinity();
    }

    ArithmeticProgression corresponding_ap() const {
        return ap;
    }
};

int main() {
    HarmonicProgression hp(1, 1);
    int n;
    cout<<"Enter number of HP terms: ";
    cin>>n;
    cout << "First " << n << " terms of HP: ";
    vector<double> hp_terms = hp.generate_terms(n);
    for (double term : hp_terms) {
        cout << term << " ";
    }
    cout << std::endl;

    cout << "Sum of first " << n << " terms of HP: " << hp.sum_to_n_terms(n) << endl;
    cout << n << "th term of HP: " << hp.nth_term(n) << endl;
    cout << "Sum of HP to infinity: " << hp.sum_to_infinity() << endl;

    ArithmeticProgression ap = hp.corresponding_ap();
    cout << "First " << n << " terms of corresponding AP: ";
    vector<double> ap_terms = ap.generate_terms(n);
    for (double term : ap_terms) {
        cout << term << " ";
    }
    cout << endl;

    cout << "Sum of first " << n << " terms of AP: " << ap.sum_to_n_terms(n) << endl;
    cout << n << "th term of AP: " << ap.nth_term(n) << endl;

    return 0;
}

Explanation:

  1. ArithmeticProgression Class:

    • Represents an arithmetic progression with the first term aa and common difference dd.
    • Provides methods to calculate the nth term, sum of the first n terms, and generate the first n terms.
    • The HarmonicProgression class is declared as a friend to allow access to its private members.
  2. HarmonicProgression Class:

    • Contains an instance of the ArithmeticProgression class.
    • Provides methods to calculate the nth term of the HP, generate the first n terms of the HP, calculate the sum of the first n terms of the HP, and calculate the sum of the HP to infinity.
    • Returns the corresponding AP using the corresponding_ap method.
  3. Main Function:

    • Demonstrates the usage of the HarmonicProgression and ArithmeticProgression classes, including generating terms, calculating sums, and accessing corresponding AP terms.

OUTPUT:


Enter number of HP terms: 5
First 5 terms of HP: 1 0.5 0.333333 0.25 0.2 
Sum of first 5 terms of HP: 2.28333
5th term of HP: 0.2
Sum of HP to infinity: inf
First 5 terms of corresponding AP: 1 2 3 4 5
Sum of first 5 terms of AP: 15
5th term of AP: 5

Tuesday, July 2, 2024

Program 2 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 2 Write a Point class that represents a 2-d point in a plane. Write member functions to a. Set and show the value of a point b. Find the distance between two points c. Check whether two points are equal or not

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 2

Write a Point class that represents a 2-d point in a plane. Write member functions to a. Set and show the value of a point b. Find the distance between two points c. Check whether two points are equal or not

SOURCE CODE:
#include <iostream>
#include <cmath>
using namespace std;

class Point {
private:
    double x, y;

public:
    // Constructor
    Point(double x = 0, double y = 0) : x(x), y(y) {}

    // Set the value of the point
    void setPoint(double x, double y) {
        this->x = x;
        this->y = y;
    }

    // Show the value of the point
    void showPoint() const {
        cout << "Point(" << x << ", " << y << ")" << std::endl;
    }

    // Find the distance between two points
    double distanceTo(const Point &other) const {
        return sqrt(std::pow(x - other.x, 2) + std::pow(y - other.y, 2));
    }

    // Check whether two points are equal or not
    bool equals(const Point &other) const {
        return x == other.x && y == other.y;
    }
};

int main() {
    Point p1(1, 2);
    Point p2(4, 6);

    p1.showPoint();
    p2.showPoint();

    cout << "Distance between points: " << p1.distanceTo(p2) << std::endl;
    cout << "Points are equal: " << (p1.equals(p2) ? "Yes" : "No") << std::endl;

    return 0;
}


OUTPUT:

Point(1, 2)
Point(4, 6)
Distance between points: 5
Points are equal: No

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...