1002. Cat



Time Limit: 1sec    Memory Limit:256MB
Description
Description
实现如下animal类的子类cat类,使得调用示例可得到对应输出。
提交时只需要提交cat类实现,不要提交animal类和main函数。
class animal{
public:
animal(int age) : age(age) {} 
int age;
};
class cat : public animal {
public:
cat(int age=1);
static int getCatNum(int age); //返回猫的数量 
//add any function or property here 
}; 

调用示例:
int main()
{
cat cat1;
cout << cat::getCatNum() << endl;
{
cat cat2(2);
cout << cat::getCatNum() << endl;
cat cat3(cat2); 
cout << cat::getCatNum() << endl;
}
cout << cat::getCatNum() << endl;

return 0;


对应输出:
1
2
3
1

考察点:
类继承;静态变量;构造函数、拷贝构造函数、析构函数;

Problem Source: 12级期末机考题

一发WA:

#include<iostream>
using namespace std;
class animal{
public:
    animal(int age) : age(age) {
        this->age = age;
    }
    animal(){
        age = 1;
    }
    int age;
};
class cat : public animal {
public:
    cat(int age = 1){
        this->age = age;
        num++;
    }
    cat(cat &other){
        num++;
        this->age = other.age;
    }
    ~cat(){
        num--;
    }
    static int getCatNum(){ //返回猫的数量 
        return num;
    }
    //add any function or property here 
private:
    static int num;
};
int cat:: num = 0;                                 


最终AC:  :public animal(age)拯救一切

class cat : public animal {
public:
	/*cat():animal(age){
		age = 1;
		num++;
	}*/
	cat(int age = 1) :animal(age){
		this->age = age;
		num++;
	}
	cat(cat &other) :animal(age){
		num++;
		this->age = other.age;
	}
	~cat(){
		num--;
	}
	static int getCatNum(){ //返回猫的数量 
		return num;
	}
	//add any function or property here 
private:
	static int num;
};
int cat:: num = 0;




conclusion:继承还有很多不懂的,期末考试要加油了。

Time Limit: 1sec    Memory Limit:256MB
Description
Description
实现如下animal类的子类cat类,使得调用示例可得到对应输出。
提交时只需要提交cat类实现,不要提交animal类和main函数。
class animal{
public:
animal(int age) : age(age) {} 
int age;
};
class cat : public animal {
public:
cat(int age=1);
static int getCatNum(int age); //返回猫的数量 
//add any function or property here 
}; 

调用示例:
int main()
{
cat cat1;
cout << cat::getCatNum() << endl;
{
cat cat2(2);
cout << cat::getCatNum() << endl;
cat cat3(cat2); 
cout << cat::getCatNum() << endl;
}
cout << cat::getCatNum() << endl;

return 0;


对应输出:
1
2
3
1

考察点:
类继承;静态变量;构造函数、拷贝构造函数、析构函数;

Problem Source: 12级期末机考题

发布了49 篇原创文章 · 获赞 15 · 访问量 8万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章