OOPs practical codes

 oops practical

SE ,AIDS practical

practical no :1

/*
Implement a class Complex which represents the Complex Number data type. Implement the following
1.  Constructor (including a default constructor which creates the complex number 0+0i).
2.  Overload operator+ to add two complex numbers.
3.  Overload operator* to multiply two complex numbers.
4.  Overload operators << and >> to print and read Complex Numbers
*/

#include <iostream>

using namespace std;

class Complex
{
    double real;
    double img;

public:
    Complex();                                              // Default Constructor
    friend istream &operator>>(istream &, Complex &);       // Input
    friend ostream &operator<<(ostream &, const Complex &); // Output
    Complex operator+(Complex);                             // Addition
    Complex operator*(Complex);                             // Multiplication
};

Complex::Complex()
{
    real = 0;
    img = 0;
}

istream &operator>>(istream &, Complex &i)
{
    cin >> i.real >> i.img;
    return cin;
}

ostream &operator<<(ostream &, const Complex &d)
{
    cout << d.real << " + " << d.img << "i" << endl;
    return cout;
}

Complex Complex::operator+(Complex c1)
{
    Complex temp;
    temp.real = real + c1.real;
    temp.img = img + c1.img;
    return temp;
}

Complex Complex::operator*(Complex c2)
{
    Complex tmp;
    tmp.real = real * c2.real - img * c2.img;
    tmp.img = real * c2.img + img * c2.real;
    return tmp;
}

int main()
{
    Complex C1, C2, C3, C4;
    int flag = 1;
    char b;
    while (flag == 1)
    {
        cout << "Enter Real and Imaginary part of the Complex Number 1 : ";
        cin >> C1;
        cout << "Enter Real and Imaginary part of the Complex Number 2 : ";
        cin >> C2;
        int f = 1;
        while (f == 1)
        {
            cout << "\nComplex Number 1 : " << C1 << endl;
            cout << "Complex Number 2 : " << C2 << endl;
            cout << "********** MENU **********" << endl;
            cout << "1. Addition of Complex Numbers" << endl;
            cout << "2. Multiplication of Complex Numbers" << endl;
            cout << "3. Exit\n";
            int a;
            cout << "\nEnter your choice from above MENU (1 to 3) : ";
            cin >> a;
            if (a == 1)
            {
                C3 = C1 + C2;
                cout << "Addition : " << C3 << endl;
                cout << "Do you wan to perform another operation (y/n) : ";
                cin >> b;
                if (b == 'y' || b == 'Y')
                {
                    f = 1;
                }
                else
                {
                    cout << "\nThanks for using this program !!\n";
                    flag = 0;
                    f = 0;
                }
            }
            else if (a == 2)
            {
                C4 = C1 * C2;
                cout << "Multiplication : " << C4 << endl;
                cout << "Do you wan to perform another operation (y/n) : ";
                cin >> b;
                if (b == 'y' || b == 'Y')
                {
                    f = 1;
                }
                else
                {
                    cout << "\nThanks for using this program !!\n";
                    flag = 0;
                    f = 0;
                }
            }
            else
            {
                cout << "\n Thanks for using this program !!\n";
                flag = 0;
                f = 0;
            }
        }
    }
    return 0;
}

Practical no:2


/*
Develop a program in C++ to create a database of student's information system containing the following information:
Name, Roll number, Class, Division, Date of Birth, Blood group, Contactaddress, Telephone number, Driving license no. and other.
Construct the database with suitable member functions.
Make use of constructor, default constructor, copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete as well as exception handling.
*/

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class PersonClass
{
private:
    char name[40], clas[10], div[2], dob[15], bloodgrp[5];
    int roll;

public:
    static int count; // static data
    friend class PersonnelClass;
    PersonClass()
    {
        char *name = new char[40];
        char *dob = new char[15];
        char *bloddgrp = new char[5];
        char *cls = new char[10];
        char *div = new char[2];
        roll = 0;
    }
    static void TotalRecordCount()
    {
        cout << "\n\nTOTAL NUMBER OF RECORDS CREATED: " << count;
    }
};

class PersonnelClass
{
private:
    char address[30], telephone_no[15], policy_no[10], license_no[10];

public:
    PersonnelClass()
    {
        strcpy(address, "");
        strcpy(telephone_no, "");
        strcpy(policy_no, "");
        strcpy(license_no, "");
    }
    void InputData(PersonClass *obj);
    void DisplayData(PersonClass *obj);
    friend class PersonClass;
};

int PersonClass::count = 0; // static data initialized using scope resolution// operator

void PersonnelClass::InputData(PersonClass *obj)
{
    cout << "\nROLLNO : ";
    cin >> obj->roll;
    cout << "NAME : ";
    cin >> obj->name;
    cout << "CLASS : ";
    cin >> obj->clas;
    cout << "DIVISION : ";
    cin >> obj->div;
    cout << "DATE OF BIRTH(DD-MM-YYYY) : ";
    cin >> obj->dob;
    cout << "BLOOD GROUP : ";
    cin >> obj->bloodgrp;
    cout << "ADDRESS : ";
    cin >> this->address;
    cout << "TELEPHONE NUMBER : ";
    cin >> this->telephone_no;
    cout << "DRIVING LICENSE NUMBER : ";
    cin >> this->license_no;
    cout << "POLICY NUMBER : ";
    cin >> this->policy_no;
    obj->count++;
}

void PersonnelClass::DisplayData(PersonClass *obj)
{
    cout << "\n";
    cout << obj->roll << " " << obj->name << " " << obj->clas << " " << obj->div << " " << obj->dob << " " << this->address << " " << this->telephone_no << " " << obj->bloodgrp << " " << this->license_no << " " << this->policy_no;
}

int main()
{
    PersonnelClass *a[10];
    PersonClass *c[10];
    int n = 0, i, choice;
    char ans;
    do
    {
        cout << "\nMENU: ";
        cout << "\n\t1.Input Data\n\t2.Display Data";
        cout << "\n\nEnter your choice : ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            cout << "\n\n\tENTER THE DETAILS";
            cout << "\n--------------------------";
            do
            {
                a[n] = new PersonnelClass;
                c[n] = new PersonClass;
                a[n]->InputData(c[n]);
                n++;
                PersonClass::TotalRecordCount();
                cout << "\n\nDo you want to add more records ?(y/n): ";
                cin >> ans;
            } while (ans == 'y' || ans == 'Y');
            break;
        case 2:
            cout << "\n--------------------------";
            cout << "\n Roll Name Class Div BirthDate Address Telephone Blood_Gr Licence Policy ";
            cout << "\n--------------------------";
            for (i = 0; i < n; i++)
                a[i]->DisplayData(c[i]);
            PersonClass::TotalRecordCount();
            break;
        }
        cout << "\nDo you want to go to main menu?(y/n): ";
        cin >> ans;
        cin.ignore(1, '\n');
    } while (ans == 'y' || ans == 'Y');
    return 0;
}


practical no:3

/*
Imagine a publishing company which does marketing for book and audiocassette versions.
Create a class publication that stores the title (a string) and price (type float) of a publication.
From this class derive two classes: book, which adds a page count(type int), and tape, which adds a playing time in minutes(type float).
Write a program that instantiates the book and tape classes, allows user to enter data and displays the data members.
If an exception is caught, replace all the data member values with zero values.
*/

#include <iostream>
#include <stdio.h>

using namespace std;

class publication
{
private:
    string title;
    float price;

public:
    void add()
    {
        cout << "\nEnter the Publication information : " << endl;
        cout << "Enter Title of the Publication : ";
        cin.ignore();
        getline(cin, title);
        cout << "Enter Price of Publication : ";
        cin >> price;
    }
    void display()
    {
        cout << "\n--------------------------------------------------";
        cout << "\nTitle of Publication : " << title;
        cout << "\nPublication Price : " << price;
    }
};

class book : public publication
{
private:
    int page_count;

public:
    void add_book()
    {
        try
        {
            add();
            cout << "Enter Page Count of Book : ";
            cin >> page_count;
            if (page_count <= 0)
            {
                throw page_count;
            }
        }
        catch (...)
        {
            cout << "\nInvalid Page Count!!!";
            page_count = 0;
        }
    }
    void display_book()
    {
        display();
        cout << "\nPage Count : " << page_count;
        cout << "\n--------------------------------------------------\n";
    }
};

class tape : public publication
{
private:
    float play_time;

public:
    void add_tape()
    {
        try
        {
            add();
            cout << "Enter Play Duration of the Tape (Minutes) : ";
            cin >> play_time;
            if (play_time <= 0)
                throw play_time;
        }
        catch (...)
        {
            cout << "\nInvalid Play Time!!!";
            play_time = 0;
        }
    }
    void display_tape()
    {
        display();
        cout << "\nPlay Time : " << play_time << " min";
        cout << "\n--------------------------------------------------\n";
    }
};

int main()
{
    book b1[10]; // object of class book
    tape t1[10]; // object of class tape
    int ch, b_count = 0, t_count = 0;
    do
    {
        cout << "\n* * * * * PUBLICATION DATABASE SYSTEM * * * * *";
        cout << "\n--------------------MENU-----------------------";
        cout << "\n1. Add Information to Books";
        cout << "\n2. Add Information to Tapes";
        cout << "\n3. Display Books Information";
        cout << "\n4. Display Tapes Information";
        cout << "\n5. Exit";
        cout << "\n\nEnter your choice : ";
        cin >> ch;
        switch (ch)
        {
        case 1:
            b1[b_count].add_book();
            b_count++;
            break;
        case 2:
            t1[t_count].add_tape();
            t_count++;
            break;
        case 3:
            cout << "\n* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *";
            for (int j = 0; j < b_count; j++)
            {
                b1[j].display_book();
            }
            break;
        case 4:
            cout << "\n* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *";
            for (int j = 0; j < t_count; j++)
            {
                t1[j].display_tape();
            }
            break;
        case 5:
            exit(0);
        }
    } while (ch != 5);
    return 0;
}


practical no:4


#include <iostream>
#include <fstream>

using namespace std;

class Employee
{
    string Name;
    int ID;
    double salary;

public:
    void accept()
    {
        cout << "Name : ";
        cin.ignore();
        getline(cin, Name);
        cout << "Id : ";
        cin >> ID;
        cout << "Salary : ";
        cin >> salary;
    }
    void display()
    {
        cout << "Name : " << Name << endl;
        cout << "Id : " << ID << endl;
        cout << "Salary : " << salary << endl;
    }
};

int main()
{
    Employee o[5];
    fstream f;
    int i, n;

    // Open file for writing
    f.open("demo.txt", ios::out | ios::binary); // Open the file in binary mode for writing
    if (!f)
    {
        cout << "File could not be opened for writing!" << endl;
        return 1;
    }

    cout << "\nEnter the number of employees you want to store: ";
    cin >> n;

    for (i = 0; i < n; i++)
    {
        cout << "\nEnter information of Employee " << i + 1 << "\n";
        o[i].accept();
        f.write((char *)&o[i], sizeof(o[i])); // Write object data to file
    }

    f.close(); // Close the file after writing

    // Open file for reading
    f.open("demo.txt", ios::in | ios::binary); // Open the file in binary mode for reading
    if (!f)
    {
        cout << "File could not be opened for reading!" << endl;
        return 1;
    }

    cout << "\nInformation of Employees is as follows : \n";
    for (i = 0; i < n; i++)
    {
        f.read((char *)&o[i], sizeof(o[i])); // Read object data from file
        cout << "\nEmployee " << i + 1 << "\n";
        o[i].display();
    }

    f.close(); // Close the file after reading

    return 0;
}


pratical no:5
/*
Write a function template for selection sort that inputs, sorts and outputs an integer array and a float array.
*/

#include <iostream>

using namespace std;

int n;
#define size 10
template <class T>

void sel(T A[size])
{
    int i, j, min;
    T temp;
    for (i = 0; i < n - 1; i++)
    {
        min = i;
        for (j = i + 1; j < n; j++)
        {
            if (A[j] < A[min])
                min = j;
        }
        temp = A[i];
        A[i] = A[min];
        A[min] = temp;
    }
    cout << "\nSorted array:";
    for (i = 0; i < n; i++)
    {
        cout << " " << A[i];
    }
    cout << "\n";
}

int main()
{
    int A[size];
    float B[size];
    int i;
    int ch;
    do
    {
        cout << "\n* * * * * SELECTION SORT SYSTEM * * * * *";
        cout << "\n--------------------MENU-----------------------";
        cout << "\n1. Integer Values";
        cout << "\n2. Float Values";
        cout << "\n3. Exit";
        cout << "\n\nEnter your choice : ";
        cin >> ch;

        switch (ch)
        {
        case 1:
            cout << "\nEnter total no of int elements : ";
            cin >> n;
            cout << "\nEnter int elements : ";
            for (i = 0; i < n; i++)
            {
                cin >> A[i];
            }
            sel(A);
            break;
        case 2:
            cout << "\nEnter total no of float elements : ";
            cin >> n;
            cout << "\nEnter float elements : ";
            for (i = 0; i < n; i++)
            {
                cin >> B[i];
            }
            sel(B);
            break;
        case 3:
            exit(0);
        }
    } while (ch != 3);
    return 0;
}


practical no:6

/*
Write C++ program using STL for sorting and searching user defined records such as personal records (Name, DOB, Telephone numbers, etc) using vector container.
*/

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

typedef struct rec
{
    char name[20];
    char BirthDt[20];
    char phone[11];
} node;

node temp;

vector<node> rec;
vector<node>::iterator ptr;

bool compare(node &r1, node &r2)
{
    if (strcmp(r1.name, r2.name) < 0)
        return true;
    else
        return false;
}

void Create()
{
    int n, i;
    cout << "\nHow many elements you want to insert ? ";
    cin >> n;
    cout << "\nEnter the elements in the database - " << endl;
    for (i = 0; i < n; i++)
    {
        cout << "Name : ";
        cin >> temp.name;
        cout << "Birth Date : ";
        cin >> temp.BirthDt;
        cout << "Phone : ";
        cin >> temp.phone;
        rec.push_back(temp);
        cout << "\n";
    }
}

void Display()
{
    cout << "\nThe contents of the database are - " << endl;
    cout << "Name       Birth Date      Phone" << endl;
    for (ptr = rec.begin(); ptr != rec.end(); ptr++)
    {
        cout << "\n";
        cout << "" << (*ptr).name;
        cout << "       " << (*ptr).BirthDt;
        cout << "       " << (*ptr).phone;
    }
    cout << "\n";
}

void Searching()
{
    char key[20];
    int flag = 0;
    cout << "\nEnter the name which you want to search : ";
    cin >> key;
    for (ptr = rec.begin(); ptr != rec.end(); ptr++)
    {
        if (strcmp((*ptr).name, key) == 0)
        {
            flag = 1;
            break;
        }
        else
            flag = 0;
    }
    if (flag == 1)
        cout << "The desired element is present in the database" << endl;
    else
        cout << "The desired element is not present in the database" << endl;
}

void Sorting()
{
    sort(rec.begin(), rec.end(), compare);
    cout << "\n\nRecord is sorted !" << endl;
}

int main()
{
    char ans = 'y';
    int choice;
    cout << "Program for Searching & Sorting : " << endl;

    do
    {
        cout << "\nMain Menu - " << endl;
        cout << "1. Create a database\n2. Display a database\n3. Search particular element\n4. Sort the database(based on name)" << endl;
        cout << "Enter your choice : ";
        cin >> choice;

        switch (choice)
        {
        case 1:
            Create();
            break;
        case 2:
            Display();
            break;
        case 3:
            Searching();
            break;
        case 4:
            Sorting();
            break;
        default:
            cout << "\nInvalid choice !" << endl;
            break;
        }

        cout << "\nDo you want to go back to Main Menu ? ";
        cin >> ans;
    } while (ans == 'y');
}

practical no:7

/*
Write a program in C++ to use map associative container.
The keys will be the names of states and the values will be the populations of the states.
When the program runs, the user is prompted to type the name of the state.
The program then loops in th emap, using the state name as an index and returns the population of the state.
*/

#include <iostream>
#include <map>
#include <string>
#include <utility>

using namespace std;

int main()
{
    typedef map<string, int> mapType;
    mapType populationMap;

    populationMap.insert(pair<string, float>("Maharashtra", 125));
    populationMap.insert(pair<string, float>("Uttar_Pradesh", 225));
    populationMap.insert(mapType::value_type("Bihar", 120));
    populationMap.insert(mapType::value_type("West Bengal", 100));
    populationMap.insert(make_pair("Madhya_Pradesh", 90));
    populationMap.insert(make_pair("Tamil_Nadu", 80));
    populationMap.insert(make_pair("Rajasthan", 78));
    populationMap.insert(make_pair("Andhra_Pradesh", 53));
    populationMap.insert(make_pair("Odisha", 47));
    populationMap.insert(make_pair("Kerala", 38));
    populationMap.insert(make_pair("Telangana", 37));
    populationMap.insert(make_pair("Assam", 35));
    populationMap.insert(make_pair("Jharkhand", 38));
    populationMap.insert(make_pair("Karnataka", 68));
    populationMap.insert(make_pair("Gujarat", 70));
    populationMap.insert(make_pair("Punjab", 31));
    populationMap.insert(make_pair("Chhattisgarh", 30));
    populationMap.insert(make_pair("Haryana", 29));
    populationMap.insert(make_pair("UT_Delhi", 19));
    populationMap.insert(make_pair("UT_Jammu_and_Kashmir", 14));
    populationMap.insert(make_pair("Uttarakhand", 12));
    populationMap.insert(make_pair("Himachal_Pradesh", 8));
    populationMap.insert(make_pair("Tripura", 04));
    populationMap.insert(make_pair("Meghalaya", 4));
    populationMap.insert(make_pair("Manipur", 3));
    populationMap.insert(make_pair("Nagaland", 2));
    populationMap.insert(make_pair("Goa", 2));
    populationMap.insert(make_pair("Arunachal_Pradesh", 2));
    populationMap.insert(make_pair("UT_Puducherry", 2));
    populationMap.insert(make_pair("Mizoram", 1));
    populationMap.insert(make_pair("UT_Chandigarh", 1));
    populationMap.insert(make_pair("Sikkim", 1));
    populationMap.insert(make_pair("UT_Dadra_and_Nagar_Haveli_and_Daman_and_Diu", 1));
    populationMap.insert(make_pair("UT_Andaman_and_Nicobar_Islands", 1));
    populationMap.insert(make_pair("UT_Lakshadweep", 0.0003));
    populationMap.insert(make_pair("UT_Ladakh", 0.00006));

    mapType::iterator iter = --populationMap.end();
    populationMap.erase(iter);

    cout << "Total state and UT of India with Size of populationMap: " << populationMap.size() << '\n';

    for (iter = populationMap.begin(); iter != populationMap.end(); ++iter)
    {
        cout << iter->first << " : " << iter->second << " million\n";
    }

    char c;
    do
    {
        string state;
        cout << "\nEnter that state you want to know the population of : ";
        cin >> state;
        iter = populationMap.find(state);
        if (iter != populationMap.end())
            cout << state << "'s populations is " << iter->second << " million" << endl;
        else
            cout << "State is not in Population Map\n";

        cout << "Do you wish to continue?(y/n) : ";
        cin >> c;
    } while (c == 'y' || c == 'Y');

    populationMap.clear();
    return 0;
}


Thank you !!!











Comments

Popular posts from this blog

From Data to Dashboard: Your Ultimate Guide to Power BI

Unlocking Student Potential: The Power of Data Analytics in Education