第二十讲 友元函数
定义
一个函数不是本类的函数,但可以访问本类的成员。
那需要在类中定义这个为友元函数
声明格式
friend void visit();
实验 二十讲 友元函数的使用方法
想用一个普通函数来操作定义对象的数据成员。
在类当中声明变通函数是这个类的友元函数
#include <iostream.h>
#include <string.h>//strcpy head file
class CStudent
{
public:
CStudent(); //构造函数一定和它的类名字是一样的
CStudent(char *name,int age); //可以和成员变量区分开来
CStudent(const CStudent &student);//拷贝构造函数
~CStudent();
friend CStudent &input(CStudent &student,char *name, int
age);
void initialize(char *name,int age); //赋值
void output(); //输出类对象
public:
//char m_name[20];
//int m_age;
char name[20];
int age;
};
//习惯在成员变量前面加上m_表示成员变量,以区分
//实现
CStudent::CStudent() //类很多,要用域操作符表明属于哪一个类
{
strcpy(name,"");
age = 0;
} //构造函数就是对数据成员进行初始化
CStudent::CStudent(char *name,int age)
{
strcpy(this->name,name);
this->age = age; //从形参传递过来
} //实现两个构造函数
//以上数据成员用_标示了出来,如果使用this指针的话可以用this-
>age=age来实现
void CStudent::initialize(char *name,int age)
{ strcpy(this->name,name);
this->age = age;
}
//析构函数的实现
CStudent::~CStudent()
{
cout<<"the object is deconstructing"<<endl;//作为析构的
标记
}
CStudent::CStudent(const CStudent &student)
{
strcpy(this->name,student.name);
this->age = student.age;
}
void CStudent::output()
{
cout<<name<<" "<<age<<endl;
}
//定义的外部函数,使用引用传递参数
CStudent &input(CStudent &student,char *name, int age)
{
strcpy(student.name,name);
student.age = age;
return student;
}
//定义对象
void main()
{
CStudent stu("TOM",76);
input(stu,"JIM",56);
stu.output();
}
第二十二讲 友元类
定义
一个类的所有成员能访问另 个类的所有成员,我们称这个类是别一个类
的友元类。
声明格式
friend class 类名
通过一类的对象改变另一个类的对象数据的目的。
友元类的使用方法 实验 第二十讲
点点
定义友元类
定义访问其他类的方法实现
声明是友元类
在实现中访问其他类的成员
#include <iostream.h>
#include <string.h>//strcpy head file
class CStudent
{
public:
CStudent(); //构造函数一定和它的类名字是一样的
CStudent(char *name,int age); //可以和成员变量区分开来
CStudent(const CStudent &student);//拷贝构造函数
~CStudent();
//friend CStudent &input(CStudent &student,char *name,
int age);
friend class CTeacher;
void initialize(char *name,int age); //赋值
void output(); //输出类对象
public:
//char m_name[20];
//int m_age;
char name[20];
int age;
};
//习惯在成员变量前面加上m_表示成员变量,以区分
//实现
CStudent::CStudent() //类很多,要用域操作符表明属于哪一个类
{
strcpy(name,"");
age = 0;
} //构造函数就是对数据成员进行初始化
CStudent::CStudent(char *name,int age)
{
strcpy(this->name,name);
this->age = age; //从形参传递过来
} //实现两个构造函数
//以上数据成员用_标示了出来,如果使用this指针的话可以用this-
>age=age来实现
void CStudent::initialize(char *name,int age)
{ strcpy(this->name,name);
this->age = age;
}
//析构函数的实现
CStudent::~CStudent()
{
cout<<"the object is deconstructing"<<endl;//作为析构的
标记
}
CStudent::CStudent(const CStudent &student)
{
strcpy(this->name,student.name);
this->age = student.age;
}
void CStudent::output()
{
cout<<name<<" "<<age<<endl;
}
//定义的外部函数,使用引用传递参数
/*
CStudent &input(CStudent &student,char *name, int age)
{
strcpy(student.name,name);
student.age = age;
return student;
}
*/
class CTeacher
{
public:
CStudent &visit(CStudent &student,char *name,int age );
};
CStudent &CTeacher::visit(CStudent &student,char *name,int age )
{
strcpy(student.name,name);
student.age = age;
return student;
}
//定义对象
void main()
{
CStudent stu1;
CTeacher teacher1;
teacher1.visit(stu1,"TOM",32);
stu1.output();
}
第二十四讲 静态成员变量
定义:一个类中的成员变量,它存储在静态存储区,前且被所有的类对象
使用,这样的成员变量我们称之为静态成员变量
格式
static 变量类型 变量名;
只能在类中定义
同时在类外声明,并且初始化
第二十五讲 实验 静态数据成员的使用
#include <iostream.h>
#include <string.h>//strcpy head file
class CStudent
{
public:
CStudent(); //构造函数一定和它的类名字是一样的
CStudent(char *name,int age); //可以和成员变量区分开来
CStudent(const CStudent &student);//拷贝构造函数
~CStudent();
void add(int age);
//friend CStudent &input(CStudent &student,char *name,
int age);
friend class CTeacher;
void initialize(char *name,int age); //赋值
void output(); //输出类对象
public:
//char m_name[20];
//int m_age;
char name[20];
int static age;
};
int CStudent::age = 0;
//习惯在成员变量前面加上m_表示成员变量,以区分
//实现
void CStudent::add(int age)
{
CStudent::age += age;
}
CStudent::CStudent() //类很多,要用域操作符表明属于哪一个类
{
strcpy(name,"");
age = 0;
} //构造函数就是对数据成员进行初始化
CStudent::CStudent(char *name,int age)
{
strcpy(this->name,name);
this->age = age; //从形参传递过来
} //实现两个构造函数
//以上数据成员用_标示了出来,如果使用this指针的话可以用this-
>age=age来实现
void CStudent::initialize(char *name,int age)
{ strcpy(this->name,name);
this->age = age;
}
//析构函数的实现
CStudent::~CStudent()
{
cout<<"the object is deconstructing"<<endl;//作为析构的
标记
}
CStudent::CStudent(const CStudent &student)
{
strcpy(this->name,student.name);
this->age = student.age;
}
void CStudent::output()
{
cout<<name<<" "<<age<<endl;
}
//定义的外部函数,使用引用传递参数
/*
CStudent &input(CStudent &student,char *name, int age)
{
strcpy(student.name,name);
student.age = age;
return student;
}
*/
class CTeacher
{
public:
CStudent &visit(CStudent &student,char *name,int age );
};
CStudent &CTeacher::visit(CStudent &student,char *name,int age )
{
strcpy(student.name,name);
student.age = age;
return student;
}
//定义对象
void main()
{
CStudent stu1,stu2;
CTeacher teacher1;
teacher1.visit(stu1,"TOM",32);
stu1.add(10);
stu1.output();
stu2.add(10);
stu2.output();
}