C ++中的类和对象

时间:2010-05-01 09:39:33

标签: c++ turbo-c++

class anurag
{
private:
int rollno;
char name[50];
int marks;
float percen;
void percentage(int num)   
{
 percen=(num/500)*100;

}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
gets(name);
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}
void display(void)
{
cout<<"\n\nThe name of the student is:";
cout.write(name,50);
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\n The marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen;
}};
   void main()
   {
clrscr();
anurag F;
F.getdata();
F.display();
getch();
   }

为什么以下代码没有提供所需的输出?

2 个答案:

答案 0 :(得分:7)

因为你有错误。

答案 1 :(得分:0)

#include<iostream>
#include<conio.h>
using namespace std;
class anurag
{
private:
int rollno;
string name;
int marks;
float percen;

public:
void percentage(float num)
{
  percen=(num/500)*100;

}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
cin>>name;
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}

void display(void)
{
cout<<"\n\nThe name of the student is:";
cout<<name;
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\nThe marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen<<"%";
}};
   int main()
   {
//clrscr();
anurag F;
F.getdata();
F.display();
getch();
return 0;
   }

我做了一些改变。 int num应该是float。该程序现在工作正常。 (请原谅我,如果我所做的改变是错误的。我没有编码经验。我只是试图摆脱这个错误!)