标题:c++小程序,有一个错误。求高人指点
只看楼主
杨龙龙
Rank: 2
等 级:论坛游民
帖 子:17
专家分:29
注 册:2012-3-8
结帖率:100%
已结贴  问题点数:10 回复次数:3 
c++小程序,有一个错误。求高人指点
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int ,string,float);
    void display();
private:
    int num;
    string name;
    float score;
};
Student::Student(int n,string nam,float s)
{
    num=n;
    name=nam;
    score=s;
}
void Student::display()
{
    cout<<num<<name<<score<<endl;
}
class Graduate:public Student
{
    Graduate (int ,string ,float ,float);
    void display();
private:
    float pay;
};
Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p){}
void Graduate::display()
{Student::display();
cout<<pay<<endl;
}
int main()
{
    Student stud1(1001,"liuanglong",3.3);
    Graduate grad1(2001,"dsddfkjjdfhgkjdf",33.3,33.333);
    Student *p=&stud1;
    p->display();
    p=&grad1;
    p->display();
}


搜索更多相关主题的帖子: void class private display include 
2012-03-28 23:27
yubiaohyb
Rank: 2
等 级:论坛游民
帖 子:6
专家分:12
注 册:2012-3-28
得分:0 
Student(int ,string,float);
 这是一个明显的错误吧  大哥
Student::Student(int n,string nam,float s)
 {
     num=n;
     name=nam;
     score=s;
 }
 字符串的处理应该调用字符串函数吧:strcpy(name, nam);其他的  不多说 你再自己试试吧
2012-03-28 23:51
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
得分:10 
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int ,string,float);
    void display();
private:
    int num;
    string name;
    float score;
};

Student::Student(int n,string nam,float s)
{
    num=n;
    name=nam;
    score=s;
}

void Student::display()
{
    cout<<num<<name<<score<<endl;
}

class Graduate:public Student
{
public:
    Graduate (int ,string ,float ,float);
    void display();
private:
    float pay;
};

Graduate::Graduate(int n,string nam,float s,float p)
    :Student(n,nam,s),pay(p){}

void Graduate::display()
{
    Student::display();
    cout<<pay<<endl;
}

int main()
{
    Student stud1(1001,"liuanglong",3.3f);
    Graduate grad1(2001,"dsddfkjjdfhgkjdf",33.3f,33.333f);

    Student *p=&stud1;
    p->display();
    p=&grad1;
    p->display();

    return 0;
}

我们都在路上。。。。。
2012-03-29 10:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-364633-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.134263 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved