在课堂上分享信息 [英] sharing information in a class

查看:73
本文介绍了在课堂上分享信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我在上课时遇到麻烦。我在函数CreateCircle中使用

成功调用getvolume(),但它不想在ShowCircle()

函数中调用它。我住在同一个班级。我认为那是

封装的重点。当调用ShowCircle()函数时,我会变得很大

编号-1.07374e + 008

有人可以帮助我吗?

class Circle < br $>
{

public:


Circle(int radius){

itsRadius = 0; }

void CreateCircle();

void ShowCircle();


void SetVolume(float radius){

volume = PI * pow(radius,2); }

float GetVolume(){return volume; }


私人:

浮动itsRadius;

浮动量;

};

void Circle :: CreateCircle()//画一个圆圈

{char color;

浮动itsRadius,音量;


cout<<"输入以cm为单位的半径: ;

cin>> itsRadius;


SetVolume(itsRadius);

cout<<"卷: << GetVolume()<< endl;

}

void Circle :: ShowCircle()

{int i;

cout<<"卷是: <<< GetVolume()<<"" cm cube << endl;

}

解决方案

Dan写道:

课堂电话有问题。我在函数CreateCircle中调用了getvolume(),但是它不想在ShowCircle()
函数中调用它。我住在同一个班级。我认为这是封装的重点。当函数ShowCircle()被调用时我得到很大的数字-1.07374e + 008
任何人都可以帮助我吗?

class Circle
{
public:

Circle(int radius){
itsRadius = 0; }


当你构造一个Circle时,为什么不初始化音量呢?

void CreateCircle();
void ShowCircle();

void SetVolume(浮点半径){
volume = PI * pow(radius,2); }


在此功能中设置音量,但''itsRadius''永远不会改变

(并且保持为0)。这是故意的吗?


什么是PI?如何定义?

float GetVolume(){return volume;私有:
浮动itsRadius;
浮动卷;
};

void Circle :: CreateCircle()//绘制一个圈子
{char color;
float itsRadius,volume;

cout<<"输入以cm为单位的半径: ;
cin>> itsRadius;


如果输入半径,将成员

变量设置为具有相同的值是不是有意义?


注意,你在成员函数中。你在这里声明了两个

_LOCAL_变量''itsRadius''和''volume''。它们与_member_变量具有相同的名称,因此_hide_数据

成员。不知怎的,我不认为这是你的意图。

SetVolume(itsRadius);
cout<<"卷: << GetVolume()<< endl;
}

void Circle :: ShowCircle()
{int i;
cout<<"卷是: <<< GetVolume()<<"" cm cube << endl;
}




我(我也相信其他人)想看看你怎么称呼它们

功能。仅仅看到类定义和

实现是不够的,总是要看看如何使用类。


我怀疑是你使用不同的圈子却没有意识到

吧。


Victor


class Circle
{
公开:

Circle(int radius){
itsRadius = 0;当你构造一个Circle时,为什么不初始化音量呢?



好​​的我会

void CreateCircle() ;
void ShowCircle();

void SetVolume(float radius){
volume = PI * pow(radius,2); }
在此功能中设置音量,但''itsRadius''永远不会改变
(并保持0)。这是故意吗?



有一个功能设定音量,半径值返回计算

音量

<什么是PI?它是如何定义的?



在课程定义之前它是程序第一行的常量

PI = 3.14;

float GetVolume(){return volume;私有:
浮动它的天堂;
浮动卷;
};

void Circle :: CreateCircle()//画一个圈子
{char color;
float itsRadius,volume;

cout<<"输入以cm为单位的半径: ;
cin>> itsRadius;



如果输入半径,将成员
变量设置为具有相同的值是不是有意义?

请注意,您在成员函数中。你在这里声明了两个
_LOCAL_变量''itsRadius''和''volume''。它们具有与_member_变量相同的名称,因此_hide_数据成员。不知怎的,我不认为这是你的意图。


SetVolume(itsRadius);
cout<<<"卷: << GetVolume()<< endl;
}

void Circle :: ShowCircle()
{int i;
cout<<"卷是: <<< GetVolume()<<"" cm cube << endl;
}



我(也确定其他人)希望看到你如何称呼这些功能。看到课程定义和实现是不够的,人们总是要看看课程是如何使用的。

我怀疑你是否使用不同的圈子而没有意识到/>它。

Victor




我(我相信其他人也希望看到你如何称呼这些功能。看到类定义和
实现是不够的,总是要看看如何使用类。




好​​的,这里是我的完整计划。它编译,但我说的答案是

错误:


#include< iostream>

#include< cmath> ;

使用命名空间std;


const double PI = 3.14159;


class Point

{int x,y,color;


public:

Point(int a = 0,int b = 0,int c = 0) {

x = a; Y = B;颜色= C; }

};


class Shape

{protected:

Point p1;


public:

Shape(Point p):p1(p){}


Shape(){};

int SetColor();

void CreateShape(); //选择形状

void DrawShape();

void DisplayShape();

受保护:


};


class Circle:public Shape

{

public:

Circle (){};

Circle(Point p,int r = 0):Shape(p){itsRadius = r;}

Circle(int radius){

itsRadius = 0;

volume = 0;

}

void CreateCircle();

void ShowCircle();


void SetVolume(浮点半径){

volume = PI * pow(radius,2); }

float GetVolume(){return volume; }


私人:


浮动itsRadius;

浮动量;

} ;


void Shape :: CreateShape()

{Circle circ;

char choice;

//圈circle_array [10];

// int count_circle = 0;


do {

cout<< "形状管理系统<< endl;

cout<< " =============================================="< < endl;

cout<< endl<< endl;

cout<<" 1.创建一个圆圈"<< endl;

cout<<"" 2.创建一个圆柱体"<< endl;

cout<<"" 3.创建一个三角形<< endl;

cout<<"" 4.返回主菜单<< endl;

cout<< "您的选择请: ;

cin>>选择;


开关(选择)

{

case''1'':

circ.CreateCircle();

休息;

案例''2'':


休息;

案例''3'':


休息;

案例''4'':cout<<"此退出将返回到上一个菜单! << endl;

休息;

默认值:cout<<"错误:无效选项,请再试一次 << endl;

休息;

}

} while((choice!='''4''));

}


void Shape :: DisplayShape()

{Circle circ;


char选择;


做{

cout<<""形状管理系统<< endl;

cout<< " =============================================="< < endl;

cout<< endl<< endl;

cout<<" 1.显示创建的圈子"<<< endl;

cout<<"" 2.显示创建的圆柱体"<<< endl;

cout<<"" 3.显示创建的三角形<< endl;

cout<<" 4.返回主菜单<< endl;

cout<< "您的选择请: ;

cin>>选择;


开关(选择)

{

case''1'':

circ.ShowCircle(); // SetTime(& s [0],& count,& totalTime [0]);

break;

case''2'':


休息;

案例''3'':


休息;

案例''4'':cout<<"这个退出将返回上一个菜单! << endl;

休息;

默认值:cout<<"错误:无效选项,请再试一次 << endl;

休息;

}


} while((选择!=''4'') );

}


无效菜单()

{char choice;

圆圈抽奖;


float * count = 0;


do {

cout<< endl<<" ;形状管理系统<< endl;

cout<< " =============================================="< < endl;

cout<<" 1.创建对象"<< endl;

cout<<"" 2.显示创建对象"<<< endl;

cout<<"" 3.退出"<< endl;

cout<< "您的选择请: ;

cin>>选择;


开关(选择)

{

case''1'':

draw.CreateShape(); // SetTime(& s [0],& count,& totalTime [0]);

break;

case''2'':

draw.DisplayShape();

break;

case''3'':cout<<"谢谢你使用过这个系统,再见!!!

休息;

默认:cout<<"错误:选项无效,请再试一次 << endl<< endl;

}


} while((选择!=''3''));

}


float main()

{

menu();

getch() ;

返回0;

}


void Circle :: CreateCircle()//画一个圆圈

{Circle cir(0);

char color;

float itsRadius;


cout<<"以cm为单位输入半径: ;

cin>> itsRadius;


cir.SetVolume(itsRadius);


cout<<"卷: << cir.GetVolume()<< endl;


color = SetColor();

}


void Circle :: ShowCircle()

{

cout<<""卷是: << GetVolume()<<" cm cube << endl;


}


int Shape :: SetColor()//基类的函数

{char input;


cout<<"从以下菜单中选择一种颜色: << endl;

cout<<" 1.蓝色<<< endl;

cout<<"" 2.绿色<<< endl;

cout<<" 3.红色<<< endl;

cout<<"" 4.橙色<<< endl;

cout<<" 5.黄色"<< endl;

cout<<" 6.紫色"<< endl;


cin>>输入;

if(输入==''1''||输入==''2''||输入==''3''||输入=='''4'' ||输入

==''5''|| input ==''6'')返回输入;

else

{ cout<<"你没有选择颜色,请选择1到

6之间的数字! << endl;

}


返回0;

}


Hello,

I have trouble with class calling. I am calling getvolume() with succes in
the function CreateCircle but it do not want to call it in ShowCircle()
function. I am staying in the same class. I thought that was the point of
encapsulation. When the function ShowCircle() is called I get very large
number -1.07374e+008
can anyone help me ?
class Circle
{
public:

Circle (int radius){
itsRadius = 0; }
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
float GetVolume(){ return volume; }

private:
float itsRadius;
float volume;
};
void Circle::CreateCircle() //Draw a circle
{ char color;
float itsRadius, volume;

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;

SetVolume(itsRadius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCircle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}

解决方案

Dan wrote:

I have trouble with class calling. I am calling getvolume() with succes in
the function CreateCircle but it do not want to call it in ShowCircle()
function. I am staying in the same class. I thought that was the point of
encapsulation. When the function ShowCircle() is called I get very large
number -1.07374e+008
can anyone help me ?
class Circle
{
public:

Circle (int radius){
itsRadius = 0; }
When you construct a Circle, why not initialise the volume as well?
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
In this function you set the volume, but ''itsRadius'' never changes
(and stays 0). Is that intentional?

What''s "PI"? How is it defined?
float GetVolume(){ return volume; }

private:
float itsRadius;
float volume;
};
void Circle::CreateCircle() //Draw a circle
{ char color;
float itsRadius, volume;

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;
If you enter the radius, wouldn''t it make sense to set the member
variable to have the same value?

Notice, that you''re in the member function. You declared two
_LOCAL_ variables here ''itsRadius'' and ''volume''. They have the
same names as _member_ variable, and therefore _hide_ the data
members. Somehow I don''t think that was your intention.

SetVolume(itsRadius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCircle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}



I (and am sure others too) would like to see how you call those
functions. It''s not enough to see the class definition and the
implementation, one always has to see how the class is used.

My suspicion is that you use different circles without realising
it.

Victor


class Circle
{
public:

Circle (int radius){
itsRadius = 0; }
When you construct a Circle, why not initialise the volume as well?


Ok I will

void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
In this function you set the volume, but ''itsRadius'' never changes
(and stays 0). Is that intentional?


There is a function Set volume, that the radius value is return to calculate
the volume


What''s "PI"? How is it defined?


It is a constant in the First line of the program before class definition
PI= 3.14;

float GetVolume(){ return volume; }

private:
float itsRadius;
float volume;
};

void Circle::CreateCircle() //Draw a circle
{ char color;
float itsRadius, volume;

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;



If you enter the radius, wouldn''t it make sense to set the member
variable to have the same value?

Notice, that you''re in the member function. You declared two
_LOCAL_ variables here ''itsRadius'' and ''volume''. They have the
same names as _member_ variable, and therefore _hide_ the data
members. Somehow I don''t think that was your intention.


SetVolume(itsRadius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCircle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}



I (and am sure others too) would like to see how you call those
functions. It''s not enough to see the class definition and the
implementation, one always has to see how the class is used.

My suspicion is that you use different circles without realising
it.

Victor





I (and am sure others too) would like to see how you call those
functions. It''s not enough to see the class definition and the
implementation, one always has to see how the class is used.



Ok so here is my full program. it compiles but the answer like I says is
wrong:

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;

class Point
{ int x,y, color;

public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c; }
};

class Shape
{ protected:
Point p1;

public:
Shape(Point p) :p1(p) {}

Shape(){};
int SetColor();
void CreateShape(); //choosing the shape
void DrawShape();
void DisplayShape();
protected:

};

class Circle : public Shape
{
public:
Circle (){};
Circle(Point p, int r=0) : Shape(p) {itsRadius = r;}
Circle (int radius){
itsRadius = 0;
volume =0;
}
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
float GetVolume(){ return volume; }

private:

float itsRadius;
float volume;
};

void Shape::CreateShape()
{ Circle circ;
char choice;
// Circle circle_array[10];
//int count_circle =0;

do {
cout<<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout <<endl <<endl;
cout<<" 1. Create a Circle "<<endl;
cout<<" 2. Create a Cylinder "<<endl;
cout<<" 3. Create a Triangle "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case ''1'':
circ.CreateCircle() ;
break;
case ''2'':

break;
case ''3'':

break;
case ''4'': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}
}while ( (choice != ''4'') ) ;
}

void Shape::DisplayShape()
{ Circle circ;

char choice;

do {
cout<<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout <<endl <<endl;
cout<<" 1. Show Circles created "<<endl;
cout<<" 2. Show Cylinders created "<<endl;
cout<<" 3. Show Triangles created "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case ''1'':
circ.ShowCircle() ; //SetTime( &s[0], &count, &totalTime[0] );
break;
case ''2'':

break;
case ''3'':

break;
case ''4'': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}

}while ( (choice != ''4'') ) ;
}

void menu()
{ char choice;
Circle draw;

float *count =0;

do {
cout<<endl <<" Shape Management System "<<endl;
cout<< " ============================================== "<<endl;
cout<<" 1. Create Object "<<endl;
cout<<" 2. Display Object Created "<<endl;
cout<<" 3. Quit "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case ''1'':
draw.CreateShape(); //SetTime( &s[0], &count, &totalTime[0] );
break;
case ''2'':
draw.DisplayShape();
break;
case ''3'': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" << endl <<endl;
}

}while ( (choice != ''3'') ) ;
}

float main()
{
menu();
getch();
return 0;
}

void Circle::CreateCircle() //Draw a circle
{ Circle cir(0);
char color;
float itsRadius;

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;

cir.SetVolume(itsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCircle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor() //Functions for the base class
{ char input;

cout<<" Choose a color from the following menu: " <<endl;
cout<<" 1. Blue "<<endl;
cout<<" 2. Green "<<endl;
cout<<" 3. Red "<<endl;
cout<<" 4. Orange "<<endl;
cout<<" 5. Yellow "<<endl;
cout<<" 6. Purple "<<endl;

cin >> input ;
if (input == ''1'' || input == ''2'' || input == ''3'' || input == ''4'' || input
== ''5'' || input == ''6'') return input ;
else
{ cout<<"You have not choosen a color, Please choose a number between 1 to
6 !" <<endl;
}

return 0;
}



这篇关于在课堂上分享信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆