Array of Objects & Passing Objects as Function Arguments in C++
// #include<iostream>
// using namespace std;
// class Employee {
// int id;
// int salary;
// public:
// void getId(void){
// cout<<"enter the id of the employee";
// cin>>id;
// }
// void setId(void){
// cout<<"the id of the employee is "<<id<<endl;
// }
// };
// int main(){
// Employee krishna, shalini, krisha, shana;
// Employee ks[4]; //an array which stores data of 4
// for (int i = 0; i<=4; i++)
// {
// ks[4].getId();
// ks[4].setId();
// }
//krishna.getId();
//krishna.setId();
//suppose i have a company named ks and it has 2000 employees and i need to store
//id and salary of each empoyee so doing this separately would not be feasible
//so we need to make an array
// return 0;
// }
#include<iostream>
using namespace std;
class complex{
int a;
int b;
public:
void setData(int v1, int v2)
{
a = v1;
b = v2;
}
void setDataBySum(complex o1, complex o2)
{
a = o1.a + o2.a;
b = o1.b + o2.b;
}
void PrintNumber(void)
{
cout<<"your completx number is "<<a<<"+"<<b<<"i"<<endl;
}
};
int main(){
complex c1,c2,c3;
c1.setData(1, 2);
c1.PrintNumber();
c2.setData(3, 4);
c2.PrintNumber();
c3.setDataBySum(c1, c2);
c3.PrintNumber();
return 0;
}
Comments
Post a Comment