Моя цель до написания этого кода состояла в том, чтобы просто попрактиковаться и узнать больше о C ++.
Код состоит из класса ball , который имеет такие свойства шара, какцвет, размер, вес, а также «бренд» и цена мяча.
#include<iostream>
#include<string>
using namespace std;
class ball
{
private:
int price; //Price of a ball in rupees .
enum colour { red , green , blue } colour; // Colour of the ball .
string brand; // Brand of the ball REEBOK ADIDAS etcetera .
float size; // Diameter of the ball .
enum weight { light , medium , heavy }weight; // Qualitative weight .
public:
ball();
void get_price();
void get_colour();
void get_brand();
void get_size();
void get_weight();
};
ball::ball() : price(0) , brand(NULL) , size(0.0)
{
cout<<"In the constructor";
colour=blue;
weight=medium;
}
void ball::get_price()
{
cout<<"In the function get_price()"<<endl<<price<<endl;
}
void ball::get_colour()
{
cout<<"In the function get_colour()"<<endl<<colour<<endl;
}
void ball::get_brand()
{
cout<<"In the function get_brand()"<<endl<<brand<<endl;
}
void ball::get_size()
{
cout<<"In the function get_size()"<<endl<<size<<endl;
}
void ball::get_weight()
{
cout<<"In the function get_weight()"<<endl<<weight<<endl;
}
int main()
{
ball glace;
glace.get_price();
glace.get_colour();
glace.get_brand();
glace.get_size();
glace.get_weight();
}
Проблема возникла с использованием enums в определении класса.Изначально я получил такие ошибки, как C2436, C2275, C2064. Все ошибки при каждой компиляции были связаны с перечислением .После исправления, наконец, приведенный выше код ошибка компиляции бесплатно!Но это дает мне ошибку во время выполнения.!!
Может кто-нибудь объяснить мне, почему?
PS: Я использую Microsoft Visual C ++ 2005 Express Edition.