我们可以通过 C++ 中的运算符重载将一个 class 数据转换为另一个 class 数据吗?
can we convert one class data into another class by operator overloading in c++?
我有 2 个 classes Employee
和 Person
。员工 class 具有三个属性 name
、age
和 salary
。人 class 具有属性 name
和 age
。我想重载赋值运算符以将 Employee class 的姓名和年龄分配给 name 和 age person class.
class Employee {
string name;
int age;
float salary;
public:
Employee()
{
name="";
age=0;
salary=0;
}
void operator =(const Employee& a)
{
name=a.name;
age=a.age;
}
};
class Person {
string name;
int age;
public:
Person()
{
name="";
age=0;
}
void display()
{
cout<<"Name are :"<<name<<endl;
cout<<"Age are :"<<age<<endl;
}
};
int main()
{
Employee obj;
person obj1;
obj=obj1;
}
以上问题的答案
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
int age;
float salary;
public:
// default constructor (meaning less)
Employee() {
name = "";
age = 0;
salary = 0.0;
}
// Parameterized constructor
Employee(string name, int age, float salary) {
this->name = name;
this->age = age;
this->salary = salary;
}
// getter members to
// get value of private members
// from outside class
string getName() {
return this->name;
}
int getAge() {
return this->age;
}
float getSalary() {
return this->salary;
}
};
class Person: Employee {
private:
string name;
int age;
public:
Person() {
name = "";
age = 0;
}
void operator = (Employee &emp) {
this->name = emp.getName();
this->age = emp.getAge();
}
void display() {
cout << "Name is : " << name << endl;
cout << "Age is : " << age << endl;
}
};
int main() {
Employee emp("Emp", 25, 45000.00);
Person p1;
p1 = emp;
p1.display();
return 0;
}
我有 2 个 classes Employee
和 Person
。员工 class 具有三个属性 name
、age
和 salary
。人 class 具有属性 name
和 age
。我想重载赋值运算符以将 Employee class 的姓名和年龄分配给 name 和 age person class.
class Employee {
string name;
int age;
float salary;
public:
Employee()
{
name="";
age=0;
salary=0;
}
void operator =(const Employee& a)
{
name=a.name;
age=a.age;
}
};
class Person {
string name;
int age;
public:
Person()
{
name="";
age=0;
}
void display()
{
cout<<"Name are :"<<name<<endl;
cout<<"Age are :"<<age<<endl;
}
};
int main()
{
Employee obj;
person obj1;
obj=obj1;
}
以上问题的答案
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
int age;
float salary;
public:
// default constructor (meaning less)
Employee() {
name = "";
age = 0;
salary = 0.0;
}
// Parameterized constructor
Employee(string name, int age, float salary) {
this->name = name;
this->age = age;
this->salary = salary;
}
// getter members to
// get value of private members
// from outside class
string getName() {
return this->name;
}
int getAge() {
return this->age;
}
float getSalary() {
return this->salary;
}
};
class Person: Employee {
private:
string name;
int age;
public:
Person() {
name = "";
age = 0;
}
void operator = (Employee &emp) {
this->name = emp.getName();
this->age = emp.getAge();
}
void display() {
cout << "Name is : " << name << endl;
cout << "Age is : " << age << endl;
}
};
int main() {
Employee emp("Emp", 25, 45000.00);
Person p1;
p1 = emp;
p1.display();
return 0;
}