Educn class date int d m y public dateint dd0 int

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .init(16,10,1996); my_birthday.init(30,12,1950); Date tomorrow = today; tomorrow.add_day(); //… } //定义成员函数时使用的方式 void Date::init(int dd, int mm, int yy) { d = dd; // d是结构内部的成员 m = mm; y = yy; } // d m y 是函数被调用时所针对的 那个对象的成员 Beijing University of Posts & Telecommunications Beijing [email protected] 类定义 class X { … }; 被称为一个类定义,因为它定义了一个新类 型 由于历史的原因,一个类定义也常常被说成 是一个类声明 就像其他不是定义的声明一样,类定义可以 由于#include的使用而在不同的源文件里 重复出现,这样并不违反唯一定义规则 Beijing University of Posts & Telecommunications Beijing [email protected] 10.2.2 访问控制 通过struct Date声明,提供了一组操作 Date的函数,然而,它却没有清楚地说明 这些函数就是直接依赖于Date地表示的全 部函数,而且也是仅有的能够直接访问 Date类的对象的函数 这些限制可以通过用class替代struct而描 述清楚 Beijing University of Posts & Telecommunications Beijing [email protected] 访问控制 标号public将这个类内部分成两个部分,其中第一部分(私用部分)只能由 成员函数使用;而第二部分(共用部分)则构成了该类的对象的公用界面, struct也是class,不过其成员的默认方式是共用的 class Date { int d, m, y; public: void init (Date& d, int, int, int); }; void add_year(Date& d,int n); void add_month(Date& d, int n); void add_day(Date& d, int n); Beijing University of Posts & Telecommunications Beijing // 函数定义同前 void Date::add_year(int n) { y += n; } [email protected] 访问控制 //非成员函数将禁止访问私有成员 void timewrap(Date& d) { d.y -= 200; // Error } 这种限定的好处是: 1 导致日期异常的错误必然是由某个成员函数造成的 2 一个潜在的用户要...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern