为什么这样写就是对的,也就是说允许有私有变量,但不许有私有方法么??

#include <iostream>

using namespace std;

enum BREED (Golden,Cairn, Lab, Schnauzer); 

struct Mammal{

Mammal(): Age(0),Weight(0){};
~Mammal(){};

virtual void setAge(int age){this->Age=age;}
virtual int getAge(){return Age;}
virtual void setWeight(int weight){this->Weight=weight;}
virtual int getWeight(){return Weight;}
protected: int Age,Weight;

};

struct Dog:public Mammal{

Dog(): Mammal(){};
~Dog();
BREED getBreed(){
return breed;
}
void setBreed(BREED b){
this->breed=b;
}
private BREED breed;

void Bark(){
cout<<"Bark!!"<<endl;
}  

};

int main(){

Dog Argo;
Argo.Bark();
cout<<Argo.getAge()<<endl;;

system("pause");
return 0;
}
这段代码哪里错了,怎么改正?另附:枚举的用法是怎样,枚举列的名字是否可以当成一个类来使用??
void Bark(){
cout<<"Bark!!"<<endl;
}  
private :BREED breed;

动漫人物
浏览 67回答 2
2回答

慕田峪4524236

#include <iostream>#include <stdlib.h>using namespace std;enum BREED {Golden,Cairn, Lab, Schnauzer}; //不是 ()而是{ }&nbsp;struct Mammal{Mammal(): Age(0),Weight(0){};~Mammal(){};virtual void setAge(int age){this->Age=age;}virtual int getAge(){return Age;}virtual void setWeight(int weight){this->Weight=weight;}virtual int getWeight(){return Weight;}protected: int Age,Weight;};struct Dog:public Mammal{Dog(){breed=Lab;};//这里有错&nbsp;~Dog(){};BREED getBreed(){return breed;}void setBreed(BREED b){this->breed=b;}BREED breed;//把private去掉就行了&nbsp;&nbsp;void Bark(){cout<<"Bark!!"<<endl;}&nbsp;&nbsp;};int main(){Dog Argo;&nbsp;&nbsp;Argo.Bark();cout<<Argo.getAge()<<endl;;system("pause");return 0;}如果一个变量只有几种可能的值,可以定义为枚举类型。枚举类型就是将变量的值一一列举出来,变量的值仅限于列举出来的值的范围内。enum {sun, mon, tue, wed, thu, fri, sat} workday, weekend ;其中sun, mon,....,sat称为枚举元素或枚举常量,为用户定义的标识符,所代表的意义由用户决定,在程序中体现出来。1、枚举元素为常量,不可赋值运算。 sun=0; mon=1;2、在定义枚举类型的同时,编译程序按顺序给每个枚举元素一个对应的序号,序号从0开始,后续元素依次加1。3、可以在定义时人为指定枚举元素的序号值。enum weekday {sun=9, mon=2, tue, wed, thu, fri, sat};4、只能给枚举变量赋枚举值,若赋序号值必须进行强制类型转换。&nbsp;

月关宝盒

#include "iostream"using namespace std;class CPU{public:CPU(int a,int b,double c);CPU(CPU &);int getfr(){ return frequency; }int getwo(){ return wordlength; }double getco(){ return coefficient; }void run(){ cout<<"run"<<endl; }void stop(){ cout<<"stop"<<endl; }private:int frequency;// 枚举体中的类型别名表中使用的是标识符,不能使用数字// 倘若W8、W16等都没有赋值的则W8、W16等值分别为0,1,2,3,4,5,6// 倘若{W8,W16=16,W16,W32,W64,W128,W256}则值分别为0,16,17,18,19,20enum cpu_len {W8=8,W16=16,W32=32,W64=64,W128=128,W256=126}wordlength;double coefficient;};CPU::CPU(int a,int b,double c){frequency=a;// 在不改变你原有枚举体隐藏属性的前提下修改如下switch(b){case 1 : wordlength = W8; break;case 2 : wordlength = W16; break;case 3 : wordlength = W32; break;case 4 : wordlength = W64; break;case 5 : wordlength = W128; break;case 6 : wordlength = W256; break;default : break;}coefficient=c;cout<<"构造函数被调用"<<endl;}CPU::CPU(CPU &L):frequency(L.frequency),wordlength(L.wordlength),coefficient(L.coefficient){cout<<"拷贝构造函数被调用"<<endl;}int main(){CPU cpu1(4,1,4.0);CPU cpu2(cpu1);cout<<"cpu1"<<endl;cout<<"主频:"<<cpu1.getfr()<<"MHz"<<"字长:"<<cpu1.getwo()<<"倍频:"<<cpu1.getco()<<endl;cout<<"cpu2"<<endl;cout<<"主频:"<<cpu2.getfr()<<"MHz"<<"字长:"<<cpu2.getwo()<<"倍频:"<<cpu2.getco()<<endl;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP