第二周课后实践(2)

/*
 *Copyright(c)2014,烟台大学计算机学院
 *Allrights reserved.
 *文件名称:MADE2.cpp
 *作    者:张生栋
 *完成日期:2015年3月18日
 *问题描述:略
 *输入描述:略
 *输出描述:略
 *版 本 号:v1.0
 */
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
public:
    void set_data(int n, char *p,char s);
    void display( );
private:
    int num;
    char name[20];
    char sex;
};
void Student::set_data(int n, char *p,char s)
{
    num=n;
    strcpy(name,p);
    sex=s;
}
void Student::display( )
{
    cout<<"num: "<<num<<endl;
    cout<<"name: " <<name<<endl;
    cout<<"sex: " <<sex<<endl;
}
int main()
{
    Student stud1,stud2;
    stud1.set_data(1,"He",'f');
    stud2.set_data(2,"She",'m');
    stud1.display();
    stud2.display();
    return 0;
}
//概括这种写法的特点_在类外定义函数_______。
//?在类定义中,公共成员在前,私有成员在后,有何好处?_这样可以让用户将注意力集中在能被外界调用的成员上,使阅读者的思路更加清晰。__
//?成员函数的实现写在类定义之外,有何好处?__不但可以减少类体的长度,使类清晰,便于阅读,而且能使类的接口和类的实现细节分离,
//用户只能看到函数原型,而看不到函数的执行的细节,隐藏了函数的执行细节,会提高软件工程的质量。______
//?将第5行public: 去掉,记录出现的问题_
//D:\tt\main.cpp||In function 'int main()':|
//D:\tt\main.cpp|25|error: 'void Student::set_data(int, char*, char)' is private|
//D:\tt\main.cpp|40|error: within this context|
//D:\tt\main.cpp|40|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
//D:\tt\main.cpp|25|error: 'void Student::set_data(int, char*, char)' is private|
//D:\tt\main.cpp|41|error: within this context|
//D:\tt\main.cpp|41|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
//D:\tt\main.cpp|31|error: 'void Student::display()' is private|
//D:\tt\main.cpp|42|error: within this context|
//D:\tt\main.cpp|31|error: 'void Student::display()' is private|
//D:\tt\main.cpp|43|error: within this context|
//||=== Build finished: 8 errors, 2 warnings (0 minutes, 0 seconds) ===|
//_____,原因是_去掉public后,系统默认成员函数为私有的,所以不能再类外被调用,会出现错误____。加上public,将程序改回正确状态。
//?将第18行void Student::display( )写作为void display( ),即去掉Student::,结果会是_D:\tt\main.cpp||In function 'void display()':|
//D:\tt\main.cpp|33|error: 'num' was not declared in this scope|
//D:\tt\main.cpp|34|error: 'name' was not declared in this scope|
//D:\tt\main.cpp|35|error: 'sex' was not declared in this scope|
//D:\tt\main.cpp||In function 'int main()':|
//D:\tt\main.cpp|40|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
//D:\tt\main.cpp|41|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
//||=== Build finished: 3 errors, 2 warnings (0 minutes, 0 seconds) ===|
//____?Student::的作用是_限定display()函数是该类中的函数____。将程序改回正确状态。
//?在第30行后加一句:stud1.num=3,记录出现的情况,并解释原因。____||=== Build: Debug in (2) (compiler: GNU GCC Compiler) ===|
//D:\C++\di2zhou\(2)\main.cpp||In function 'int main()':
//D:\C++\di2zhou\(2)\main.cpp|10|error: 'int Student::num' is private___
//•因为num是私有的成员,不能再类外被调用。
//_____
//?去掉刚加的那一行,将第31行stud1.display();中的stud1.去掉,记录出现的情况,并解释原因。D:\C++\di2zhou\(2)\main.cpp|31|error: 'display' was not declared in this scope,disply没有被定义,要使用类内的成员函数,必须加”stud1.“因为"stud1"是类的对象。
//____
//________
//?在32行后增加cout<<sizeof(stud1)语句,看输出的结果,请做出解释多输出了“28”,这是stud1中的属性“num,sex,name”的占的字节的长度___?
//?初学者常将类定义后的分号丢掉,试将13行最后的分号去掉,记录出现的提示,并做出解释。_D:\C++\di2zhou\(2)\main.cpp|13|error: expected ';' after class definition
//类的定义是一个独立的句子,所以结束后腰加分号。
//________
//?你能想到的其他“坏”点子继续折腾吗?折腾出真知!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值