第二章 面向对象的基本概念

1.类的定义示例

#include <iostream>
#include <string>
using namespace std;

 class myDate{
     public:
        myDate(); //构造函数
        myDate(int,int,int);//构造函数
        void setDate(int,int,int);//设置日期
        void setDate(myDate);//设置日期
        myDate getDate();//获取日期
        void setYear(int);//获取年
        int getMonth();//获取月
        void printDate() const;//打印日期
    private:
        int year,month,day;//成员变量,表示年,月,日
 };
 
 //在类体外定义成员函数
myDate::myDate(){
    year=1978,month=1,day=1;
}
myDate::myDate(int y,int m,int d){
    year=y;month=m;day=d;
}    
void myDate::setDate(int y,int m,int d){
    year=y;month=m;day=d;
    return;
}
void myDate::setDate(myDate oneD){
    year=oneD.year;month=oneD.month;day=oneD.day;
    return;
}
myDate myDate::getDate(){
    return *this;
}
void myDate::setYear(int y){
    year=y;
    return;
}
int myDate::getMonth(){
    return month;
}

void myDate::printDate() const{
    cout<<year<<"/"<<month<<"/"<<day;
    return;
}

class Student{
    public:
        void setStudent(string,myDate);//设置学生信息
        void setName(string);//设置姓名
        string getName();//获取姓名
        void setBirthday(myDate);//设置生日
        myDate getBirthday();//获取生日
        void printStudent() const;//打印信息
    
    private :
        string name;//姓名
        myDate birthday;//生日
};
//在类体外定义成员函数
void Student::setStudent(string s,myDate d){
    name =s;
    birthday.setDate(d);
    return;
    
}
void Student::setName(string n){
    name =n;
    return;
}
string Student::getName(){
    return name;
}
void Student::setBirthday(myDate d){
    birthday.setDate(d);
    return;
}
myDate Student::getBirthday(){
    return birthday;
}
void Student::printStudent() const{
    cout<<"姓名:"<<name<<"\t生日:";
    birthday.printDate();
    cout<<endl;
}

int main(){
    Student ss;
    int y,m,d;
    string name_;
    cout<<"请输入学生的姓名和生日。生日以\"年月日\"的次序输入:"<<endl;
    cin>>name_>>y>>m>>d;
    ss.setStudent(name_,myDate(y,m,d));
    ss.printStudent();
    return 0;
}

分析:这里是使用对象访问成员变量与调用成员函数。

2.使用指针访问对象的成员

这里沿用上面的代码,只需要将main函数改一下

int main (){
    Student ss;
    int y,m,d;
    string name_;
    Student *sp=&ss;//指向ss的指针sp
    cout<<"请输入学生的姓名和生日。生日以\"年月日\"的次序输入:"<<endl;
    cin>>name_>>y>>m>>d;
    sp->setStudent(name_,myDate(y,m,d));
    sp->printStudent();
    return 0;
}

分析:使用指针->成员名来访问对象的成员

 3.使用引用访问对象的成员

int main(){
    Student ss;
    int y,m,d;
    string name_;
    Student &sy = ss;//ss的别名sy
    cout<<"请输入学生的姓名和生日。生日以\"年月日\"的次序输入:"<<endl;
    cin>>name_>>y>>m>>d;
    sy.setStudent(name_,myDate(y,m,d));
    sy.printStudent();
    return 0;

}

分析:引用名.成员名

输出:

  4.类成员的访问

#include <iostream>
using namespace std;
class Box{
    public:
        double length;
        void setWidth(double wid);
        double getWidth();
    private:
        double width;
};
//类体外定义成员函数
double Box::getWidth(){
    return width;
}
void Box::setWidth(double wid){
    width=wid;
}

 int main(){
     Box b;
     b.length=10.0;  
     // b.width=100.0 //错误,因为width是私有
     cout<<"b的长度是:"<<b.length<<endl;
     b.setWidth(20.0);//使用成员函数设置宽度
     cout<<"b设置的长度是:"<<b.getWidth()<<endl;
     return 0;
 }

输出:

b的长度是:10
b设置的长度是:20

 

posted @ 2020-09-22 10:16  做一只热爱生活的小透明  阅读(105)  评论(0)    收藏  举报