- 创建类和对象(声明,定义)
- 访问类成员(句点运算符,私有公有成员,存取器函数,函数的定义格式)
- 构造函数,析构函数–目的是什么?
- const成员函数
- 内联实现,探索结构
思考 - 句点运算的作用?在哪儿还见过?
- 声明和定义哪个会分配内存?
- 类声明是类的接口还是实现?
- 公有和私有数据成员区别?
- 成员函数可以私有吗?成员数据可以公有吗?
- 调用什么函数初始化对象
//Demonstrates declaration of a class and
//definition of an object of the class
#include<iostream>
class Cat
{
public: //公有成员
int GetAge(); //存取器函数:访问类的数据,易于维护
void SetAge (int age); //带有一个参数(局部变量,用完会释放内存),在主函数中用
void Meow(); //成员函数
private:
int itsAge;
};
//GetAge,Public accessor function
//returns value of itsAge member
int Cat::GetAge() //前面类中有声明,所以必须要有int GetAge();的函数体,也就是定义,
//格式如右 返回值类型int + 类名Cat + 双冒号:: + 函数名GetAge()
{
return itsAge;
}
//definition of SetAge to
//accessor function
//sets itsAge member
void Cat::SetAge(int age)
{
itsAge = age;
}
void Cat::Meow()
{
std::cout << "meow./n";
}
int main()
{
Cat Frisky;
Frisky.SetAge(5);
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge ()<< "yeas old./n";
Frisky.Meow();
return 0;
}
//Demonstrates declaration of a class and
//definition of an object of the class
#include<iostream>
class Cat
{
public: //公有成员
Cat(int initialAge); //声明构造函数(为了初始化对象),无返回值类型,连void也没有
~Cat();
int GetAge(); //存取器函数:访问类的数据,易于维护
void SetAge (int age); //带有一个参数(局部变量,用完会释放内存),在主函数中用
void Meow(); //成员函数
private:
int itsAge; //成员数据
};
//constructor of Cat
Cat::Cat(int initialAge) //定义构造函数,无返回值类型,连void也没有
{
itsAge = initialAge;
}
Cat::~Cat() //析构函数,takes no action
{
}
//GetAge,Public accessor function
//returns value of itsAge member
int Cat::GetAge() //前面类中有声明,所以必须要有int GetAge();的函数体,也就是定义,
//格式如右 返回值类型int + 类名Cat + 双冒号:: + 函数名GetAge()
{
return itsAge;
}
//definition of SetAge to
//accessor function
//sets itsAge member
void Cat::SetAge(int age)
{
itsAge = age;
}
void Cat::Meow()
{
std::cout << "meow.\n";
}
int main()
{
Cat Frisky(5); //初始化了一个参数,因为Cat构造函数接受了一个参数,所以声明/创建对象时要加个()写个参数
//Frisky.SetAge(5); 有了前面构造函数的初始化作用,这个存取器函数暂时不用
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge ()<< " yeas old.\n";
Frisky.Meow();
Frisky.SetAge(7); //前面有初始化,这儿要改变成员数据的值时,用到了存取器函数
std::cout << " Now, Frisky is a cat who is ";
std::cout << Frisky.GetAge() << " yeas old.\n";
return 0;
}
const成员函数
格式如 int GetAge() const;
使用关键字const来声明不能修改的变量,或不能修改其所属类数据的类方法(函数)
通常讲只读取存取器函数声明为const函数
将类声明和方法定义放在什么地方
类声明放在 .h 文件,配合内联实现,真正做到了类的实现细节只有类的作者关心,类的客户(即使用类的那部分函数)不需要知道;也不关心函数是如何实现的
内联实现
类方法的内联:需在返回类型前加上关键字inline.
inline int Cat::GetWeight()
{
return itsWeight;
}
也可以将类方法的定义放在类声明中,这样函数将自动成为内联的。
这是Cat.h文件
#include<iostream>
//#include "main.cpp"
class Cat
{
public:
Cat(int initialAge);
~Cat();
int GetAge() const { return itsAge; }
void SetAge(int age) { itsAge = age; }
void Meow() const { std::cout << "Meow.\n"; }
private:
int itsAge;
};
这是Cat.cpp文件
#include<iostream>
#include"Cat.h"
Cat::Cat(int initialAge) //定义构造函数,无返回值类型,连void也没有
{
itsAge = initialAge;
}
Cat::~Cat() //析构函数,takes no action
{
}
int main()
{
Cat Frisky(5); //初始化了一个参数,因为Cat构造函数接受了一个参数,所以声明/创建对象时要加个()写个参数
//Frisky.SetAge(5); 有了前面构造函数的初始化作用,这个存取器函数暂时不用
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge ()<< " yeas old.\n";
Frisky.Meow();
Frisky.SetAge(7); //前面有初始化,这儿要改变成员数据的值时,用到了存取器函数
std::cout << " Now, Frisky is a cat who is ";
std::cout << Frisky.GetAge() << " yeas old.\n";
return 0;
}
类中类(将其他类用作成员数据的类)
如发动机类,轮胎类,离合器类嵌入到汽车类中
矩形,线段,点
Cat.h
#include<iostream>
class Point
{
public:
void SetX(int x) { itsX = x; }
void SetY(int y) { itsY = y; }
int GetX()const { return itsX; }
int GetY()const { return itsY; }
private:
int itsX;
int itsY;
};
class Rectangle
{
public:
Rectangle(int top, int left, int bottom, int right); //矩形类的构造函数,与类同名,没有返回值,连void也没有
~Rectangle() {}; //析构函数
int GetTop() const { return itsTop; } //声明并在类中实现方法,const方式声明(因为不改变所属类的成员数据)+内联实现
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }
Point GetUpperLeft() const { return itsUpperLeft; } //存取器函数
Point GetLowerLeft() const { return itsLowerLeft; }
Point GetUpperRight() const { return itsUpperRight; }
Point GetLowerRight() const { return itsLowerRight; }
void SetUpperLeft(Point Location) { itsUpperLeft = Location; }
void SetLowerLeft(Point Location) { itsLowerLeft = Location; }
void SetUpperRight(Point Location) { itsUpperRight = Location; }
void SetLowerRight(Point Location) { itsLowerRight = Location; }
void SetTop(int top) { itsTop = top; }
void SetLeft(int left) { itsLeft = left; }
void SetBottom(int bottom) { itsBottom = bottom; }
void SetRight(int right) { itsRight = right; }
int GetArea() const;
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Rectangle.cpp
#include"Cat.h"
Rectangle::Rectangle(int top, int left, int bottom, int right) //构造函数函数体
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
itsUpperLeft.SetX(left); //设置四个顶点坐标
itsUpperLeft.SetY(top);
itsUpperRight.SetX(right);
itsUpperRight.SetY(top);
itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);
itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
int Rectangle::GetArea()const //Rectangle类的方法
{
int Width = itsRight - itsLeft;
int Heigh = itsTop - itsBottom;
return(Width * Heigh);
}
int main()
{
Rectangle MyRectangle(100, 20, 50, 80); //构造函数中有4个参数。所以这里创建对象时要写出这4个参数的具体值
//这一行之前,没有分配任何内存,实际上什么也没有发生,只是告诉了编译器如何创建点和矩形
int Area = MyRectangle.GetArea();
std::cout << "Area is:" << Area << ".\n";
std::cout << "Upper Left X Coordinate: ";
std::cout << MyRectangle.GetUpperLeft().GetX() << ".\n"; //GetUpperLeft()是类Rectangle中返回值类型为Point的方法,
//该方法返回一个Point,我想通过这个Point来获取其X坐标,而GetX() 是Point类的存器器函数。
std::cout << "Thank You";
return 0;
}
注
1. 程序的任何部分均可访问公有成员,私有成员只能由类的成员函数访问,如存取器函数
2.同一个类的不同对象(实例)不能彼此访问对方的私有成员
3.不应将所有数据成员都声明为共有的,这有利于程序维护(可以修改类程序而不用用户区修改其使用程序)
4.类是由struct结构发展来的,结构中没有方法