我怎么写这个程序请帮帮我 [英] How i write this program Please help me

查看:63
本文介绍了我怎么写这个程序请帮帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个程序由一个名为Student的类组成,该类应该由

组成,包括三个数据成员Name,Ob_marks,Total_marks和两个

成员函数Cal_percentage()通过公式(Ob_marks * 100)/ Total_marks和

显示()显示学生的所有信息,计算学生的百分比。该类应该包含默认构造函数,它初始化所有数据

类的成员。

在主程序中定义一个对象学生班并使用此对象调用

成员函数。

解决方案

mo ************** @ gmail.com 写道:


a程序由一个名为Student的类组成,该类应该是

由三个数据成员组成Name,Ob_marks,Total_marks和两个

成员函数Cal_percentage()通过公式(Ob_marks * 100)/ Total_marks和

Display()计算学生的百分比

显示所有信息学生。该类应该包含默认构造函数,它初始化所有数据

类的成员。

在主程序中定义一个对象学生班并使用此对象调用

成员函数。

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2


-

[我的电子邮件中没有x']


我有权利保持沉默

(并且应该尽可能多地使用它)

我打字的任何东西都可以用来对付我

in a白痴法庭

我有权利出错

(可能是)

如果我不能提出自己的错误
我相信有人会为我提供。


6月12日下午5:35,Devon Null< theronnights ... @ xgmailx.comwrote:

mohammaditrad ... @ gmail.com写道:


a程序由一个名为Student的类组成,该类应

由三个数据成员Name,Ob_marks,Total_marks和两个

成员函数Cal_percentage()组成,通过公式计算学生的百分比
(Ob_marks * 100)/ Total_marks和

Display()显示学生的所有信息。该类应该包含默认构造函数,它初始化所有数据

类的成员。

在主程序中定义一个对象学生班并使用此对象调用

成员函数。


http://www.parashift.com/c++-faq-lit...t.html#faq-5.2


-

[我的电子邮件中没有x']


我有权保持沉默

(应该可能尽可能多地使用它)

我打字的任何东西都可以用来对付我

在白痴法庭上

我有权利是错的

(可能是)

如果我不能提出自己的错误

我肯定会有人提供它我。



只复制并粘贴此程序并运行


#include< iostream>

#include< conio>

#include< string>

class学生

{

private:

char * name;

int Ob_marks;

int otal_marks;

public:

student();

学生(char&,int);

void cal_percentage();

void display()

};

student :: student()

{

int len = strlen(name);

name = new char [len + 1];

ob_marks = 0;

}

student :: sudent(char& n,int om)

{

int len = strlen(n);

name = new char [len + 1];

strcpy(name,n);

Ob_marks = om;

}

void student :: cal_percentage()< br $>
{

otal_marks =(ob_marks * 100)/ total_marks;

}

void display();

{

cout<< name<<"" \t"<< ob_marks<<" \t"<< otal_marks<< endl;

}

int main()

{

学生s1(mohammad,67);

s1.cal_percentage();

s1 .display();

返回0;

}


Pramod写道:


6月12日下午5:35,Devon Null< theronnights ... @ xgmailx.comwrote:


> mohammaditrad ... @ gmail.com写道:


>>一个程序由一个名为Student的类组成,该类应该由三个数据成员Name,Ob_marks,Total_marks和两个
成员函数Cal_percentage()组成,它们计算学生的百分比。公式(Ob_marks * 100)/ Total_marks和
Display()显示学生的所有信息。该类应该还包含默认的构造函数,它初始化类的所有数据。
在主程序中定义Student类的一个对象并调用
成员函数这个对象。


http://www.parashift.com/c++-faq-lit...t.html#faq-5.2



只复制并粘贴此程序并运行


#include< iostream>

#include< conio>



我的系统上没有conio(我应该吗?)


#include< ;字符串>

班级学生

{

私人:

char * name;



在这里使用一个字符串


int Ob_marks;



恕我直言,你可能应该为了便于输入而制作这个小写字母,

即ob_marks


int otal_marks;

public:

student();

Student(char&,int);



这里定义了2个构造函数 - 这可能是合法的,我把那个

留给比我更了解的人,但它不会编译在我的

系统上。你也应该在这里定义默认值 - 见下文。


void cal_percentage();

void display()



缺少分号


};

student :: student()



Typo here - C ++区分大小写。当您定义学生时(大写'S''),您正试图使用​​学生(较低的

案例''')。无论如何,整个

块应该被取消


{

int len = strlen(name) ;



不需要,使用sizeof(名称)


name = new char [len + 1];

ob_marks = 0;



与之前定义的Ob_marks不匹配(区分大小写)


}

student :: sudent(char& n,int om)



Student :: Student(string n,int om)


{

int len = strlen(n);

name = new char [len + 1];

strcpy(name,n);

Ob_marks = om;

}



关于此整块应重新工作


void student :: cal_percentage()



区分大小写


{

otal_marks =(ob_marks * 100)/ total_marks;



total_marks?你的意思是otal_marks? (如上所述),否则

total_marks从未被定义。


}

void display();



没有分号到这里 - 删除它


{

cout<< name<<" \t"<< ob_marks<<" \t"<< otal_marks<< endl;



必须使用std :: for cout和endl,因为你之前没有声明你的

命名空间


}

int main()

{

学生s1(mohammad,67) ;



再次,区分大小写


s1.cal_percentage();

s1.display();

返回0;

}



现在这是一个工作版本(at至少在我的系统上):


#include< iostream>

// #include< conio>

#include < string>


class学生

{

私人:

std :: string name ;

int ob_marks;

int otal_marks;

public:

// Student();

Student(std :: string n ="",int om = 0);

void cal_percentage();

void display();

};

/ *学生::学生()

{

int len = sizeof(name) ;

name = new char [len + 1];

ob_marks = 0;

} * /

学生::学生(std :: string n,int om)

{

// int len = sizeof(n);

/ / name = new char [len + 1];

// strcpy(name,n);

name = n;

ob_marks = om;

}

void Student :: cal_percentage()

{

otal_marks =(ob_marks * 100)/ otal_marks;

}

void Student :: display()

{

std :: cout<< name<<" \t"<< ob_marks<< " \t"<< otal_marks< < std :: endl;

}

int main()

{

学生s1(" mohammad",67);

s1.cal_percentage();

s1.display();

返回0;

}


现在只剩下一个问题 - 你永远不会将otal_marks设置为有意义的

值。


任何人都想批评我的建议,我也是初学者。


DN

-

[没有x在我的电子邮件中]


我有权保持沉默

(并且应该尽可能多地使用它)

我打字的任何东西都可以用来对付我

在白痴法庭上

我有权利出错

(并且可能是)

如果我不能提出自己的错误

我相信有人会为我提供它。


a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.

解决方案

mo**************@gmail.com wrote:

a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

--
[there are no x''s in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.


On Jun 12, 5:35 pm, Devon Null <theronnights...@xgmailx.comwrote:

mohammaditrad...@gmail.com wrote:

a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.


http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

--
[there are no x''s in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.

only copy and paste this programmm and run

#include <iostream>
#include <conio>
#include <string>
class Student
{
private:
char *name;
int Ob_marks;
int otal_marks;
public:
student();
Student(char&,int);
void cal_percentage();
void display()
};
student::student()
{
int len=strlen(name);
name=new char[len+1];
ob_marks=0;
}
student::sudent(char &n,int om)
{
int len=strlen(n);
name=new char[len+1];
strcpy(name,n);
Ob_marks=om;
}
void student::cal_percentage()
{
otal_marks=(ob_marks*100)/total_marks;
}
void display();
{
cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<endl ;
}
int main()
{
student s1("mohammad",67);
s1.cal_percentage();
s1.display();
return 0;
}


Pramod wrote:

On Jun 12, 5:35 pm, Devon Null <theronnights...@xgmailx.comwrote:

>mohammaditrad...@gmail.com wrote:

>> a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.


http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

only copy and paste this programmm and run

#include <iostream>
#include <conio>

I didn''t have conio on my system (should I?)

#include <string>
class Student
{
private:
char *name;

use a string here

int Ob_marks;

IMHO you should probably should make this lower case for ease of typing,
i.e. ob_marks

int otal_marks;
public:
student();
Student(char&,int);

there are 2 constructors defined here - that maybe legal, I leave that
to others more knowledgeable than me, but it would not compile on my
system. Also you should define the default values here - see below.

void cal_percentage();
void display()

missing semi-colon

};
student::student()

Typo here - C++ is case sensitive. You are trying to use student (lower
case ''s'') when you defined Student (upper case ''S''). Anyways, this whole
block should be done away with

{
int len=strlen(name);

not needed, use sizeof(name)

name=new char[len+1];
ob_marks=0;

doesn''t match the Ob_marks defined earlier (case sensitive)

}
student::sudent(char &n,int om)

Student::Student(string n, int om)

{
int len=strlen(n);
name=new char[len+1];
strcpy(name,n);
Ob_marks=om;
}

just about this whole block should be re-worked

void student::cal_percentage()

case sensitive

{
otal_marks=(ob_marks*100)/total_marks;

total_marks? you mean otal_marks? (as defined above), otherwise
total_marks was never defined.

}
void display();

no semi-colon goes here - remove it

{
cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<endl ;

have to use std:: for cout and endl since you didn''t declare your
namespace earlier

}
int main()
{
student s1("mohammad",67);

again, case sensitive

s1.cal_percentage();
s1.display();
return 0;
}

Now here is a working version (at least on my system):

#include <iostream>
//#include <conio>
#include <string>

class Student
{
private:
std::string name;
int ob_marks;
int otal_marks;
public:
//Student();
Student(std::string n = "",int om = 0);
void cal_percentage();
void display();
};
/*Student::Student()
{
int len=sizeof(name);
name=new char[len+1];
ob_marks=0;
}*/
Student::Student(std::string n,int om)
{
//int len=sizeof(n);
//name=new char[len+1];
//strcpy(name,n);
name = n;
ob_marks = om;
}
void Student::cal_percentage()
{
otal_marks=(ob_marks*100)/otal_marks;
}
void Student::display()
{
std::cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks< <std::endl;
}
int main()
{
Student s1("mohammad",67);
s1.cal_percentage();
s1.display();
return 0;
}

Now only one problem remains - you never set otal_marks to a meaningful
value.

Anyone want to critique my advice, I am also a beginner.

DN
--
[there are no x''s in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.


这篇关于我怎么写这个程序请帮帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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