大四中软实习笔记20130228

1 访问权限
public:
  当前的成员变量和成员函数,可以被本类/它类的的成员函数、外部函数(包括main函数和自定义函数)来调用
private:
  当前的成员变量和成员函数,只能被本类的成员函数来调用
protected;
  当前的成员变量和成员函数,只能被本类/派生类的成员函数来调用
 
 


2 初始化表
当需要调用它类的构造函数时,考虑使用(如果不使用,就需要使用普通的公共成员函数)
使用方法:
    在函数原型的后面,添加初始化表语句(注意,在类的声明中不能加初始化表语句)
原因:
    构造函数不能被显示调用,只能在定义对象时自动调用(包括无参构造函数和有参构造函数)   


#include
#include
class score
{
private:
    int math;
    int english;
public:
    score();
    score(int mat,int eng);
    void print();
};

score:: score()
{
    math=100;
    english=100;
    printf("score的无参构造函数被调用了\n");
}

score:: score(int mat,int eng)
{
    math=mat;
    english=eng;
    printf("score的有参构造函数被调用了\n");
}

void score::print()
{
    printf("math is %d\tenglish is %d\n",math,english);
    return;
}


class student
{
public:
    int number;
    char *name;
    int age;
    score chengji;
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag,int mat,int eng);
    student(student &other);
    ~student();
};

student:: student()
{
    number=0;
    name=new char[1];
    strcpy(name,"");
    //chengji.math=100;
    //chengji.english=100;
    //score(100,100);
    age=0;
}

student:: student(student &other)
{
    number=other.number;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;

}

student & student:: operator=(student &other)
{
    if(this==&other)
        return *this;
    number=other.number;
    char *namebak=name;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
    delete []namebak;
    return *this;
}

student:: ~student()
{
    printf("开始析构了\n");
    delete []name;
}

student:: student(int num,char *nam,int ag,int mat,int eng):chengji(mat,eng)
{
    number=num;
    name=new char[strlen(nam)+1];
    strcpy(name,nam);
    age=ag;
    //chengji.math=mat;
    //chengji.english=eng;
    //socre(mat,eng);
}

void student:: print()
{
    printf("%d\t%s\t%d\n",number,name,age);   
    chengji.print();
    return;
}

int main()
{
    //student stu1(1001,"zhangsan",21);
    //student stu2(1002,"lisi",22);
    //student stu3(1003,"wangwu",23);
    //stu3=stu2=stu1;
    //stu3.operator=(stu2);

    //stu3.print();

    student stu21(3001,"chuanda",32,91,92);
    stu21.print();


    return 0;
}

 

2 new的另外的用法
score *chengji=new score; //自动调用类的构造函数初始化。
score *chengji=new score(81,82);

score *chengji=new score[1];
score *chengji=new score[1](81,82);//编译失败
score *chengji=new score[2](81,82);//编译失败
为什么一定要定义类的无参构造函数?
原因:否则,无法使用new score[2]这样的用法

3 类和类之间的关系
3.1 UML中的解释
class student
{
public:
    int number;
    char *name;
    int age;
    score *chengji;
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag,int mat,int eng);
    student(student &other);
    ~student();
};
本例1中:student和score是聚合关系


class student
{
public:
    int number;
    char *name;
    int age;
    score chengji;
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag,int mat,int eng);
    student(student &other);
    ~student();
};
本例2中:student和score是组合关系

聚合、组合都属于关联 关系。它们是关联的2个特例


3.2 在很多程序员当中这样解释
本例1中:student和score是关联关系
本例2中:student和score是组合关系

 

 

1 自由组合
7-9人
2 选项目
  2个组可以有相同的
3 组长选好
4 人员分为ABC
5 项目要真实的
6 项目要重视质量---健壮性 

 

4 继承关系
代码;

#include
#include
class student
{
private:
    int number;
    char *name;
    int age;
public:
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag);
    student(student &other);
    ~student();
};

student:: student()
{
    number=0;
    name=new char[1];
    strcpy(name,"");
    age=0;       
}

student:: student(student &other)
{
    number=other.number;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
}

student & student:: operator=(student &other)
{
    if(this==&other)
        return *this;
    number=other.number;
    char *namebak=name;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
    delete []namebak;
    return *this;
}

student:: ~student()
{
    printf("student开始析构了\n");
    delete []name;
}

student:: student(int num,char *nam,int ag):number(num),age(ag)
{
    //number=num;
    name=new char[strlen(nam)+1];
    strcpy(name,nam);
}

void student:: print()
{
    printf("%d\t%s\t%d\n",number,name,age);   
    return;
}


class student_college:public student
{
private:
    char * name_college;
    char * name_prof;
    int count_persons;
public:
    student_college();
    ~student_college();
};

student_college::student_college()
{
    name_college=new char;
    name_prof=new char;
    strcpy(name_college,"");
    strcpy(name_prof,"");
    count_persons=0;
   
}

student_college::~student_college()
{
    delete []name_college;
    delete []name_prof;
}


int main()
{
    student_college stu_col;   

    return 0;
}

 

代码2:
#include
#include
class student
{
private:
    int number;
    char *name;
    int age;
public:
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag);
    student(student &other);
    ~student();
};

student:: student()
{
    number=0;
    name=new char[1];
    strcpy(name,"");
    age=0;   
    printf("学生类的无参构造函数被调用\n");
}

student:: student(student &other)
{
    number=other.number;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
}

student & student:: operator=(student &other)
{
    if(this==&other)
        return *this;
    number=other.number;
    char *namebak=name;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
    delete []namebak;
    return *this;
}

student:: ~student()
{
    printf("student开始析构了\n");
    delete []name;
}

student:: student(int num,char *nam,int ag):number(num),age(ag)
{
    //number=num;
    name=new char[strlen(nam)+1];
    strcpy(name,nam);
}

void student:: print()
{
    printf("%d\t%s\t%d\n",number,name,age);   
    return;
}


class student_college:public student
{
private:
    char * name_college;
    char * name_prof;
    int count_persons;
public:
    student_college();
    ~student_college();
};

student_college::student_college()
{
    name_college=new char;
    name_prof=new char;
    strcpy(name_college,"");
    strcpy(name_prof,"");
    count_persons=0;
    printf("大学生的无参构造函数被调用\n");
}

student_college::~student_college()
{
    delete []name_college;
    delete []name_prof;
    printf("大学生的析构函数被调用\n");
}


int main()
{
    student_college *stu_col=new student_college;   
    delete stu_col;

    return 0;
}

注意点:
1 使用new建立的对象,也必须使用delete来销毁。
如果,不delete,则会内存泄露。(析构函数不会被自动调用)
2 析构函数前面都要加virtual
原因:使用多态特性的时候,如果基类的析构函数前面没有virtual,则可能导致析构不全的问题。


代码3:
#include
#include
class student
{
private:
    int number;
    char *name;
    int age;
public:
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag);
    student(student &other);
    virtual ~student();
};

student:: student()
{
    number=0;
    name=new char[1];
    strcpy(name,"");
    age=0;   
    printf("学生类的无参构造函数被调用\n");
}

student:: student(student &other)
{
    number=other.number;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
}

student & student:: operator=(student &other)
{
    if(this==&other)
        return *this;
    number=other.number;
    char *namebak=name;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
    delete []namebak;
    return *this;
}

student:: ~student()
{
    printf("student开始析构了\n");
    delete []name;
}

student:: student(int num,char *nam,int ag):number(num),age(ag)
{
    //number=num;
    name=new char[strlen(nam)+1];
    strcpy(name,nam);
    printf("学生类的有参构造函数被调用\n");
}

void student:: print()
{
    printf("%d\t%s\t%d\n",number,name,age);   
    return;
}


class student_college:public student
{
private:
    char * name_college;
    char * name_prof;
    int count_persons;
public:
    student_college();
    student_college(int num,char *nam,int ag,char *name_col,char *name_pro,int count_per);
    virtual ~student_college();
    void print();
};

student_college::student_college()
{
    name_college=new char;
    name_prof=new char;
    strcpy(name_college,"");
    strcpy(name_prof,"");
    count_persons=0;
    printf("大学生的无参构造函数被调用\n");
}

student_college:: student_college(int num,char *nam,int ag,char *name_col,char *name_pro,int count_per):student(num,nam,ag)
{
    name_college=new char[strlen(name_col)+1];
    name_prof=new char[strlen(name_pro)+1];
    strcpy(name_college,name_col);
    strcpy(name_prof,name_pro);
    count_persons=count_per;
    printf("大学生的有参构造函数被调用\n");

}

student_college::~student_college()
{
    delete []name_college;
    delete []name_prof;
    printf("大学生的析构函数被调用\n");
}

void student_college:: print()
{
    printf("%s\t%s\t%d\n",name_college,name_prof,count_persons);
    student::print();
    return ;
}


int main()
{
    //student_college *stu_col=new student_college;   
    student_college stu1(1001,"zhangsan",22,"chongda","jike",56);
    stu1.print();
    return 0;
}
注意点:
1 调用基类的有参构造函数时,需要在派生类有参构造函数的后面,使用初始化表,进行显式调用
2 派生类的成员函数,如果要调用基类的成员函数,要加(基类的)作用域。(如果不同名,则可以省略)


5 多态
代码:
#include
#include
class student
{
private:
    int number;
    char *name;
    int age;
public:
    void print();
    student & operator=(student &other);
    student();
    student(int num,char *nam,int ag);
    student(student &other);
    virtual ~student();
    virtual void fun();
};
void student:: fun()
{
    printf("student_fun()\n");
    return;
}

student:: student()
{
    number=0;
    name=new char[1];
    strcpy(name,"");
    age=0;   
    printf("学生类的无参构造函数被调用\n");
}

student:: student(student &other)
{
    number=other.number;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
}

student & student:: operator=(student &other)
{
    if(this==&other)
        return *this;
    number=other.number;
    char *namebak=name;
    name=new char[strlen(other.name)+1];
    strcpy(name,other.name);
    age=other.age;
    delete []namebak;
    return *this;
}

student:: ~student()
{
    printf("student开始析构了\n");
    delete []name;
}

student:: student(int num,char *nam,int ag):number(num),age(ag)
{
    //number=num;
    name=new char[strlen(nam)+1];
    strcpy(name,nam);
    printf("学生类的有参构造函数被调用\n");
}

void student:: print()
{
    printf("%d\t%s\t%d\n",number,name,age);   
    return;
}


class student_college:public student
{
private:
    char * name_college;
    char * name_prof;
    int count_persons;
public:
    student_college();
    student_college(int num,char *nam,int ag,char *name_col,char *name_pro,int count_per);
    virtual ~student_college();
    void print();
    virtual void fun();
};
void student_college:: fun()
{
    printf("student_college_fun()\n");
    return;
}


student_college::student_college()
{
    name_college=new char;
    name_prof=new char;
    strcpy(name_college,"");
    strcpy(name_prof,"");
    count_persons=0;
    printf("大学生的无参构造函数被调用\n");
}

student_college:: student_college(int num,char *nam,int ag,char *name_col,char *name_pro,int count_per):student(num,nam,ag)
{
    name_college=new char[strlen(name_col)+1];
    name_prof=new char[strlen(name_pro)+1];
    strcpy(name_college,name_col);
    strcpy(name_prof,name_pro);
    count_persons=count_per;
    printf("大学生的有参构造函数被调用\n");

}

student_college::~student_college()
{
    delete []name_college;
    delete []name_prof;
    printf("大学生的析构函数被调用\n");
}

void student_college:: print()
{
    printf("%s\t%s\t%d\n",name_college,name_prof,count_persons);
    student::print();
    return ;
}


int main()
{
    student *stu_col=new student_college;
    stu_col->fun();
    return 0;
}

注意点:
1 使用基类的地址变量来实现多态
2 基类和派生类的函数,成员函数原型一致,而且前面要加virtual。
3 其它情况不存在多态的问题。


6
虚函数
纯虚函数   =0     无定义
抽象类  只要有1个成员函数是纯虚函数     不能实例化(不能建立 对象)    只用来派生
实例类 可以进行实例化

7
多组合
多聚合

转载于:https://www.cnblogs.com/blueswitkey/archive/2013/03/05/2943771.html

你可能感兴趣的:(大四中软实习笔记20130228)