一个类派生的问题

piaopiaohu123 2009-05-15 10:59:32
#include <stdafx.h>
#include <iostream.h>
#include <string.h>
//using namespace std;

class Student
{
public:
Student(int n,string name)
{
num=n;
nam=name;
}

void display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
Student1(int n,sring nam,int a):Student(n, name)
{
age=a;
}
void show()
{
display();
cout<<"age:"<<age<<endl;
}
private:
int age;

};

class Student2:public Student1
{
Student2(int n,string nam,int a,int s): Student1(n, nam, a)
{
score=s;
}
void show_all()
{
show();
cout<<"score:"<<score<<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}


大家帮我看看这个程序要怎么改,改了一晚上了,思路都乱了。。。谢谢了各位,小弟分不多。
...全文
161 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
piaopiaohu123 2009-05-15
  • 打赏
  • 举报
回复
恩,试过了,加了#include <stdafx.h>
会出来这个E:\artic\msg\ChatRoom\static\static.cpp(71) : error C2629: unexpected 'class Student ('
E:\artic\msg\ChatRoom\static\static.cpp(71) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\artic\msg\ChatRoom\static\static.cpp(81) : error C2146: syntax error : missing ';' before identifier 'name'
E:\artic\msg\ChatRoom\static\static.cpp(81) : error C2501: 'string' : missing storage-class or type specifiers
E:\artic\msg\ChatRoom\static\static.cpp(81) : error C2501: 'name' : missing storage-class or type specifiers
E:\artic\msg\ChatRoom\static\static.cpp(87) : error C2629: unexpected 'class Student1 ('
E:\artic\msg\ChatRoom\static\static.cpp(87) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\artic\msg\ChatRoom\static\static.cpp(102) : error C2629: unexpected 'class Student2 ('
E:\artic\msg\ChatRoom\static\static.cpp(102) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\artic\msg\ChatRoom\static\static.cpp(115) : error C2661: 'Student2::Student2' : no overloaded function takes 4 parameters
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
或者不用包含#include <stdafx.h> 然后选择setting,在c/c++栏,选择PreCompiled headers,然后设置第一选项,选择不使用预编译头
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
#include <stdafx.h>

[Quote=引用 8 楼 piaopiaohu123 的回复:]
可是,小弟按照几位的程序,在VC6下面编译还是有点问题 fatal error C1010: unexpected end of file while looking for precompiled header directive
[/Quote]
goodname 2009-05-15
  • 打赏
  • 举报
回复
程序我是调试过的,能够运行,虽然未必是楼主要表达的意思,但是肯定不会出现这种错误。

你或者re-build一下,要么还是查查是否抄掉了什么东西。
piaopiaohu123 2009-05-15
  • 打赏
  • 举报
回复
可是,小弟按照几位的程序,在VC6下面编译还是有点问题 fatal error C1010: unexpected end of file while looking for precompiled header directive
aaaa3105563 2009-05-15
  • 打赏
  • 举报
回复
帮顶··
crst_zh 2009-05-15
  • 打赏
  • 举报
回复
楼主最大的问题是没有用virtual
goodname 2009-05-15
  • 打赏
  • 举报
回复
好像晚了一点点
chenxu_ustc 2009-05-15
  • 打赏
  • 举报
回复
如果为了锻炼下使用继承 ,类可以向你这么组织
如果真正的设计类的话 ,看你的类里面 ,并没有因为多了的数据成员score 和age 使类产生了不同的行为
一般这种情况下 ,讲score 和age 也作为他的数据成员就好了,没必要继承
goodname 2009-05-15
  • 打赏
  • 举报
回复
class Student
{
public:
Student(int n,string name)
{
num=n;
this->name=name;
}

void display()
{
cout <<"num:" <<num <<endl;
cout <<"name:" <<name <<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
public:
Student1(int n,string name,int a):Student(n, name)
{
age=a;
}
void show()
{
display();
cout <<"age:" <<age <<endl;
}
private:
int age;

};

class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s): Student1(n, nam, a)
{
score=s;
}
void show_all()
{
show();
cout <<"score:" <<score <<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}


自己对比下吧
mengde007 2009-05-15
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int n,string nam):num(n),name(nam){}


void display()
{
cout <<"num:" <<num <<endl;
cout <<"name:"<<name<<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
public:
Student1(int n,string nam,int a):Student(n, nam) { age=a; }

void show()
{
display();
cout <<"age:" <<age <<endl;
}
private:
int age;

};

class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s): Student1(n, nam, a),score(s){}

void show_all()
{
Student1::show();
cout <<"score:" <<score <<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}

nickyjay0501 2009-05-15
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
Student(int n,string name)
{
num=n;
this->name=name;
}

void display()
{
cout <<"num:" <<num <<endl;
cout <<"name:" <<name <<endl;
}
protected:
int num;
string name;
};

class Student1:public Student
{
public:

Student1(int n,string name,int a):Student(n, name)
{
age=a;
}
void show()
{
display();
cout <<"age:" <<age <<endl;
}
private:
int age;

};

class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s): Student1(n, nam, a)
{
score=s;
}
void show_all()
{
show();
cout <<"score:" <<score <<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"Li",17,89);
stud.show_all();

return 0;
}
内容概要:本文介绍了一种基于多目标粒子群算法(MOPSO)的微电网优化调度模型,综合考虑风能、光伏、储能系统、柴油发电机、燃气轮机以及与主电网之间的能量交互等多种分布式能源的协同运行。通过构建以运行成本最小化、碳排放最低化和系统可靠性最优化为目标的多目标优化模型,利用Matlab平台实现MOPSO算法求解,完成对微电网在不同运行场景下的能量管理与调度方案优化。该模型能够有效平衡经济性与环保性之间的关系,适用于含多型分布式电源的复杂微电网系统,具有较强的工程应用价值和科研参考意义; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事微电网、智能电网、综合能源系统、可再生能源集成与优化调度等领域研究的专业人士; 使用场景及目标:①用于多能源耦合微电网系统的协同优化调度研究;②支持多目标智能优化算法在能源系统中的建模与求解实践,帮助用户掌握MOPSO在实际工程问题中的应用方法;③为学术论文复现、毕业设计、科研项目开发提供完整的代码实例与技术支撑; 阅读建议:建议读者结合Matlab代码与理论文档,深入理解目标函数构建、约束条件处理及Pareto最优解集生成机制,重点关注算法参数设置、多目标权衡分析与结果可视化,并可通过调整能源配置或引入新约束进行二次开发与创新研究。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧