第2周 阅读程序-初识对象(二)

第二个面向对象的程序——同样功能的程序,提倡以下面的形式书写,这是专业人员更愿意使用的方式。

#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<<'\n';
    cout<<"name: "<<name<<'\n';
    cout<<"sex: "<<sex<<'\n';
}
int main()
{
    Student s1,s2;
    s1.set_data(1,"He",'f');
    s2.set_data(2,"She",'h');
    s1.display();
    s2.display();
    cout<<sizeof(s1);
    return 0;
}

问题:

  • 概括这种写法的特点________。
  • 将公共成员写在私有成员前;在类外定义公共成员函数。
  • 在类定义中,公共成员在前,私有成员在后,有何好处?___
  • 方便查看该类所能实现的算法。
  • 成员函数的实现写在类定义之外,有何好处?________
  • 方便查看算法,查找问题,体现结构框架;便于多人操作,提高了程序的质量。
  • 将第5行public: 去掉,记录出现的问题______,原因是_____。加上public,将程序改回正确状态。
  • 出现的问题是提示成员为私有不能被调用。原因是类中默认成员为私有。
  • cpp:14:6: error: ‘void Student::set_data(int, char*, char)‘ is private

    cpp:29:30: error: within this context

    :29:30: warning: deprecated conversion from string constant to ‘char*‘ [-Wwrite-strings]

    cpp:14:6: error: ‘void Student::set_data(int, char*, char)‘ is private

    cpp:30:31: error: within this context

    cpp:30:31: warning: deprecated conversion from string constant to ‘char*‘ [-Wwrite-strings]

    cpp:20:6: error: ‘void Student::display()‘ is private

    cpp:31:19: error: within this context

    cpp:20:6: error: ‘void Student::display()‘ is private

    cpp:32:19: error: within this context

  • 将第18行void Student::display( )写作为void display( ),即去掉Student::,结果会是_____?Student::的作用是_____。将程序改回正确状态。
  • 结果是提示类的属性没有被定义声明,student::是作用域限定符,作用是调用类内的成员。
  • cpp: In function ‘void display()‘:

    cpp:22:20: error: ‘num‘ was not declared in this scope

    cpp:23:22: error: ‘name‘ was not declared in this scope

    cpp:24:21: error: ‘sex‘ was not declared in this scope

  • 在第30行后加一句:stud1.num=3,记录出现的情况,并解释原因。______
  • 提示私有。私有函数不能在类外被调用。
  • cpp:10:9: error: ‘int Student::num‘ is private

    cpp:31:11: error: within this context

  • 去掉刚加的那一行,将第31行stud1.display();中的stud1.去掉,记录出现的情况,并解释原因。________
  • 提示函数未声明。 因为display是stud1中的成员函数
  • cpp:31:13: error: ‘display‘ was not declared in this scope
  • 在32行后增加cout<<sizeof(stud1)语句,看输出的结果,请做出解释___?
  • 结果在原有的输出上增加了输出了28。

    解释:

    将stud1的属性--num,name,sex所占的字节长度输出出来了。

  • 28=1*4+20*1+1*1+1*1+1*1+1*1
  • 初学者常将类定义后的分号丢掉,试将13行最后的分号去掉,记录出现的提示,并做出解释。_________
  • cpp:13:1: error: expected ‘;‘ after class definition
  • 类的定义本身就是语句,以分号作为结束。
  • 你能想到的其他“坏”点子继续折腾吗?折腾出真知!

(3)在阅读上面两个程序的基础上再次深入体会下面的基本概念

  • 声明一个类,可以定义出结构相同的一组对象——类是对象的抽象,对象是类的实例;

  • 类/对象中包括数据成员和成员函数两部分——类/对象实现了对程序设计中要描述的实体的封装处理:将有关的数据和操作代码封装在一个对象中,形成一个基本单位,各个对象(如程序中stud1和stud2)之间相对独立,互不干扰。

  • 信息隐蔽(imformation hiding)——将对象中某些部分(通过private限定符)对外隐蔽,即隐蔽其内部细节,只留下少量接口,以便与外界联系,接收外界的消息。

  • 封装和信息隐蔽的优点:把对象的一部分属性和功能对外界屏蔽,从外界是看不到的,甚至是不可知的,有利于数据安全,防止无关的人了解和修改数据。同时,大大降低了操作对象的复杂程度。

时间: 2024-08-14 18:57:15

第2周 阅读程序-初识对象(二)的相关文章

第五周 阅读程序 1

/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年3月25日 *版 本 号:v1.0 */ #include <iostream> using namespace std; class base { private: int m; public: base() {}; base(int m){this->m=m;} int get(){return m;}

第十一周阅读程序(5.4)

问题及代码: /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:zwj.cpp *作 者:张伟晶 *完成日期:2016年5月10日 *版 本 号:v1.0 * *问题描述:阅读程序,写出运行结果 *输入描述: *程序输出: */ #include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(a

第十周-阅读程序

阅读程序1: 问题及代码: /*copyright 计算机与控制工程学院 完成日期:2016年5月6日 作者:马艳艳 问题描述:无 输入描述:无 输出描述:结果: */ #include <iostream> using namespace std; class Data { public: Data(int i):x(i){cout<<"A";}//建立构造函数 ~Data(){ cout<<"B";}//建立析构函数 priv

第十一周阅读程序(补充)----(2)

问题及代码: /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:zwj.cpp *作 者:张伟晶 *完成日期:2016年5月10日 *版 本 号:v1.0 * *问题描述:阅读程序 *输入描述: *程序输出: */ #include<iostream> using namespace std; class A { private: int x; protected: int y; public: int z; A(int

第十一周阅读程序(2)

问题及代码: /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:zwj.cpp *作 者:张伟晶 *完成日期:2016年5月10日 *版 本 号:v1.0 * *问题描述:阅读程序,写出运行结果 *输入描述: *程序输出: */ #include<iostream> using namespace std; class A { public: A(){a=0;} A(int i){a=i;} void print(){co

第11周阅读程序(2)

问题及代码: /* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:zwj.cpp *作 者:张晴晴 *完成日期:2016年5月10日 *版 本 号:v1.0 * *问题描述:阅读程序,写出运行结果 *输入描述: *程序输出: */ #include <iostream> using namespace std; class A { public: A(){a=0;} A (int i){a=i;} void print(){

第11周阅读程序写出执行结果1(5)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年5月8号 *版 本 号 : v6.0 * *问题描述 : 阅读程序写出执行结果1(5) *输入描述 : 无 *程序输出 : */ /*********************************(a)****************************************************/ #include

第五周 阅读程序 5

/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年4月6日 *版 本 号:v1.0 */ #include<iostream> using namespace std; class myClass { public: myClass(){ number++;} ~myClass(){ number--;} static int number; //声明静态数据 }

第十三周阅读程序3:纯虚函数

问题及代码: #include <iostream> using namespace std; class Base { public: virtual void Who() =0; //纯虚函数 }; class FirstDerived:public Base { public: void Who() { cout<<"F"; } }; class SecondDerived:public Base { public: void Who() { cout&l