Pages

C++ Programming Tutorials -21(Array of Objects & Passing Objects as Function Arguments )

tut25.cpp=
/*
#include <iostream>
using namespace std;

class Employee
{
    int id;
    int salary;

public:
    void setId(void)
    {
        salary = 122;
        cout << "Enter the id of employee" << endl;
        cin >> id;
    }

    void getId(void)
    {
        cout << "The id of this employee is " << id << endl;
    }
};

int main()
{
    // Employee harry, rohan, lovish, shruti;
    // harry.setId();
    // harry.getId();
    Employee fb[4];
    for (int i = 0; i < 4; i++)
    {
        fb[i].setId();
        fb[i].getId();
    }

    return 0;
}
*/
//-------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
using namespace std;

class complex{
    int a;
    int b;

    public: 
        void setData(int v1int v2){
            a = v1;
            b = v2;
        }

        void setDataBySum(complex o1complex o2){
            a = o1.a + o2.a;
            b = o1.b + o2.b;
        }

        void printNumber(){
            cout<<"Your complex number is "<<a<<" + "<<b<<"i"<<endl;
        }
};

int main(){
    complex c1c2c3;
    c1.setData(12);
    c1.printNumber();

    c2.setData(34);
    c2.printNumber();

    c3.setDataBySum(c1c2);
    c3.printNumber();
    return 0;
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Top 10 Python Packages Every Developer Should Learn

Top 10 Python Packages Every Developer Should Learn There are more than  200,000 Python packages  in the world (and that’s just counting tho...