构造函数与析构函数(C++)
[cpp]#include <iostream>
using namespace std;
enum BREED{ black, red, white, blue};
class mam{
public:
mam();
~mam();
int get_age()const { return age; }
void set_age(int age1) {age = age1; }
int get_weight() const { return weight; }
void set_weight(int x) { weight = x; }
void speak() const { cout << "mam sound!\n"; }
void sleep() const {cout << "I'am sleeping.\n"; }
protected:
int age;
int weight;
};
class dog:public mam {
public:
dog();
~dog();
BREED get_breed()const { return its_breed; }
void set_breed(BREED breed) { its_breed = breed; }
void wag_tail() const { cout << "Tail wagging...\n";}
void beg_for_food() const { cout << "begging for food...\n";}
private:
BREED its_breed;
};
mam::mam():age(3), weight(8) {
cout << "mam constructor..." << endl;
}
mam::~mam() {
cout << "mam destructor..." << endl;
}
dog::dog():its_breed(blue) {
cout << "dog constructor..." << endl;
}
dog::~dog(){
cout << "dog destructor..." << endl;
}
int main()
{
dog fido;
fido.speak();
fido.wag_tail();
cout << "fido is " << fido.get_age() << "years old" << endl;
// cout << fido.get_breed() <<endl;
// fido.set_breed(white);
// cout << fido.get_breed() << endl;
return 0;
}
补充:软件开发 , C++ ,