gpt4 book ai didi

c++ - 通过重载 operator+ C++ 添加两个不同的对象

转载 作者:行者123 更新时间:2023-11-28 03:58:01 26 4
gpt4 key购买 nike

我一直在尝试弄清楚如何将对象 A 的私有(private)成员添加到对象 B 的私有(private)成员。

Cat 和 Dog 类都继承自基类 Animal。我有一个第三类“MyClass”,我想继承 Cat 和 Dog 类的私有(private)成员。所以在 MyClass 中,我有一个友元函数来重载 + 运算符。友元函数定义如下:

MyClass operator+(const Dog &dObj, const Cat &cObj);

我想在上面的函数中访问 dObj.age 和 cObj.age,在 main 中通过这个语句调用:

mObj = dObj + cObj;

这是对类对象的完整引用的完整源代码:

   #include <iostream>
#include <vld.h>

using namespace std;

class Animal
{
public :
Animal() {};
virtual void eat() = 0 {};
virtual void walk() = 0 {};
};

class Dog : public Animal
{
public :
Dog(const char * name, const char * gender, int age);
Dog() : name(NULL), gender(NULL), age(0) {};
virtual ~Dog();
void eat();
void bark();
void walk();

private :
char * name;
char * gender;
int age;
};

class Cat : public Animal
{
public :
Cat(const char * name, const char * gender, int age);
Cat() : name(NULL), gender(NULL), age(0) {};
virtual ~Cat();
void eat();
void meow();
void walk();
private :
char * name;
char * gender;
int age;
};

class MyClass : private Cat, private Dog
{
public :
MyClass() : action(NULL) {};
void setInstance(Animal &newInstance);
void doSomething();

friend MyClass operator+(const Dog &dObj, const Cat &cObj);

private :
Animal * action;
};

Cat::Cat(const char * name, const char * gender, int age) :
name(new char[strlen(name)+1]), gender(new char[strlen(gender)+1]), age(age)
{
if (name)
{
size_t length = strlen(name) +1;
strcpy_s(this->name, length, name);
}
else name = NULL;
if (gender)
{
size_t length = strlen(gender) +1;
strcpy_s(this->gender, length, gender);
}
else gender = NULL;
if (age)
{
this->age = age;
}
}
Cat::~Cat()
{
delete name;
delete gender;
age = 0;
}
void Cat::walk()
{
cout << name << " is walking now.. " << endl;
}
void Cat::eat()
{
cout << name << " is eating now.. " << endl;
}
void Cat::meow()
{
cout << name << " says meow.. " << endl;
}
Dog::Dog(const char * name, const char * gender, int age) :
name(new char[strlen(name)+1]), gender(new char[strlen(gender)+1]), age(age)
{
if (name)
{
size_t length = strlen(name) +1;
strcpy_s(this->name, length, name);
}
else name = NULL;
if (gender)
{
size_t length = strlen(gender) +1;
strcpy_s(this->gender, length, gender);
}
else gender = NULL;
if (age)
{
this->age = age;
}
}
Dog::~Dog()
{
delete name;
delete gender;
age = 0;
}
void Dog::eat()
{
cout << name << " is eating now.. " << endl;
}
void Dog::bark()
{
cout << name << " says woof.. " << endl;
}
void Dog::walk()
{
cout << name << " is walking now.." << endl;
}


void MyClass::setInstance(Animal &newInstance)
{
action = &newInstance;
}
void MyClass::doSomething()
{
action->walk();
action->eat();
}


MyClass operator+(const Dog &dObj, const Cat &cObj)
{
MyClass A;
//dObj.age;
//cObj.age;
return A;
}

int main()
{
MyClass mObj;
Dog dObj("B", "Male", 4);
Cat cObj("C", "Female", 5);

mObj.setInstance(dObj); // set the instance specific to the object.
mObj.doSomething(); // something happens based on which object is passed in
dObj.bark();

mObj.setInstance(cObj);
mObj.doSomething();
cObj.meow();

mObj = dObj + cObj;

return 0;
}

最佳答案

如果你想访问 Dog 的私有(private)成员,那么你的运算符(operator)必须是 Dog 的友元,而不仅仅是 Dog 的某个派生类的友元。

关于c++ - 通过重载 operator+ C++ 添加两个不同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2603576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com