用户定义类型 User-defined Datatypes classes and structs
几何向量 (Geometry Vector) 二维平面上的向量由起点和终点构成 每个点包含两个坐标 (x, y), 因此一个向量需要四个实数表示 Start= (0.9,1.5) Start= (0.4,0.8) int main() { double xstart = 0.4; double xend = 0.8; double ystart = 0.9; double yend = 1.5;
几何向量 (Geometry Vector) 传递一个向量给函数, 需要传递 4 个 double void printvector(double x0,double y0,double x1,double y1){ cout << "(" << x0 << "," << y0 << ") -> (" << x1 << "," << y1 << ")" << endl; int main( ) { double xstart = 0.4; double xend = 0.8; double ystart = 0.9; double yend = 1.5; printvector(xstart, ystart, xend, yend);
void offsetvector (double &x0, double &y0, double &x1, double &y1,double offsetx, double offsety) { x0 += offsetx; y0 += offsety; x1 += offsetx; y1 += offsety; void printvector(double x0,double y0,double x1,double y1){ cout << "(" << x0 << "," << y0 << ") -> (" << x1 << "," << y1 << ")" << endl; int main( ) { double xstart = 0.4; double xend = 0.8; double ystart = 0.9; double yend = 1.5; offsetvector(xstart, ystart, xend, yend,1.2, 2.3); printvector(xstart, ystart, xend, yend); 需要传递很多变量
类 class 用户定义类型用以将相关的信息组装在一起
类名 class Vector { double xstart ; double xend ; double ystart ; double yend ; 类 class 的定义语法 C++11 标准 class Vector { double xstart = 0.4; double xend = 0.8; double ystart = 0.9; double yend = 1.5; 表示定义一个叫做 Vector 的类, 即 Vector 是用户定义的一种新类型
类 class 的定义语法 class Vector { double xstart ; double xend ; double ystart ; double yend ; 类的属性也叫成员 表示定义一个叫做 Vector 的类, 即 Vector 是用户定义的一种新类型
Student class Student { char *name ; int stuid; 类类型 Student 具有 2 个不同类型的数据成员
类的实例 - 具体的类变量 class Student { char *name ; int stuid; void main( ){ Student stu1, stu2; stu1 stu2 name =? name =? stuid =? stuid =? 某种类类型的一个具体变量叫做这个类的一个实例
类的实例 - 具体的类变量 class Student { char *name ; int stuid; void main( ){ Student stu1, stu2; stu1.name = LiPin ; stu1 name = LiPin stu2 name =? stuid =? stuid =? 某种类类型的一个具体变量叫做这个类的一个实例
class Student { char *name ; int stuid; void main( ){ Student stu1, stu2; stu1.name = LiPin ; stu1.stuid = 1403; 类的实例 - 具体的类变量 stu1 name = LiPin stu2 name =? stuid = 1403 stuid =? 某种类类型的一个具体变量叫做这个类的一个实例
class Student { char *name ; int stuid; void main( ){ Student stu1, stu2; stu1.name = LiPin ; stu1.stuid = 1403; stu2.name = XuJie ; stu2.stuid = 1207; 类的实例 - 具体的类变量 stu1 name = LiPin stu2 name =XuJie stuid = 1403 stuid = 1207 某种类类型的一个具体变量叫做这个类的一个实例
操作成员 / 属性 (Accessing Fields) 通过 变量名. 成员名 来访问类实例的成员 ( 属性 ) clss Student { char *name ; int stuid; void main( ){ Student stu1, stu2; stu1.name = LiPin ; stu1.stuid = 1403; stu2.name = XuJie ; stu2.stuid = 1207; cout<< the name of stu1 is: << stu1.name<< \n ;
Point Vector 一个向量由 2 点 ( 起点和终点 ) 构成. class Point { double x,y; class Vector { Point start, end; void main() { Vector vec1; vec1.start.x = 3.0;
class Point { double x,y; class Vector { Point start, end; void main() { Vector vec1; vec1.start.x = 3.0; vec1.start.y = 4.0; vec1.end.x = 5.0; vec1.end.y = 6.0; Point Vector
class Point { double x,y; class Vector { Point start, end; void main() { Vector vec1; vec1.start.x = 3.0; vec1.start.y = 4.0; vec1.end.x = 5.0; vec1.end.y = 6.0; Vector vec2; vec2.start = vec1.start; vec2.start.x = 7.0; Point 类有 2 个成员 x,y Point 类的 2 个实例 start,end 作为类 Vector 的成员 Vector 类的实例 vec1. vec1 也称为类变量或对象
class Point { double x,y; class Vector { Point start, end; void main() { Vector vec1; vec1.start.x = 3.0; vec1.start.y = 4.0; vec1.end.x = 5.0; vec1.end.y = 6.0; Vector vec2; vec2.start = vec1.start; vec2.start.x = 7.0;
class Point { double x,y; class Vector { Point start, end; int main() { Vector vec; vec.start.x = 1.2; vec.end.x = 2.0; vec.start.y = 0.4; vec.end.y = 1.6;
class Point { double x,y; class Vector { Point start, end; void printvector(vector v) { cout << "(" << v.start.x << "," << v.start.y << ") -> (" << v.end.x << "," << v.end.y << ")" << endl; int main() { Vector vec; vec.start.x = 1.2; vec.end.x = 2.0; vec.start.y = 0.4; vec.end.y = 1.6; printvector(vec);
class Point { double x,y; class Vector { Point start, end; 如果不需要修改传进来的 v, 可以传值 void printvector(vector v) { cout << "(" << v.start.x << "," << v.start.y << ") -> (" << v.end.x << "," << v.end.y << ")" << endl; int main() { Vector vec; vec.start.x = 1.2; vec.end.x = 2.0; vec.start.y = 0.4; vec.end.y = 1.6; printvector(vec);
class Point { double x,y; class Vector { Point start, end; void offsetvector(vector &v, double offsetx, double offsety) { v.start.x += offsetx; v.end.x += offsetx; v.start.y += offsety; v.end.y += offsety; void printvector(vector v) { cout << "(" << v.start.x << "," << v.start.y << ") -> (" << v.end.x << "," << v.end.y << ")" << endl; int main() { Vector vec; vec.start.x = 1.2; vec.end.x = 2.0; vec.start.y = 0.4; vec.end.y = 1.6; offsetvector(vec, 1.0, 1.5); printvector(vec);
class Point { double x,y; class Vector { Point start, end; 如果要修改传进来的 v, 则传引用 void offsetvector(vector &v, double offsetx, double offsety) { v.start.x += offsetx; v.end.x += offsetx; v.start.y += offsety; v.end.y += offsety; void printvector(vector v) { cout << "(" << v.start.x << "," << v.start.y << ") -> (" << v.end.x << "," << v.end.y << ")" << endl; int main() { Vector vec; vec.start.x = 1.2; vec.end.x = 2.0; vec.start.y = 0.4; vec.end.y = 1.6; offsetvector(vec, 1.0, 1.5); printvector(vec);
类的函数成员 ( 方法 ) 类的方法 (Method) 是一种属于类的函数, 用于访问类的属性 ( 数据成员 ) Vector vec; vec.start.x = 1.2; vec.end.x = 2.0; vec.start.y = 0.4; vec.end.y = 1.6; vec.print(); vec.offset( 1.0, 1.5); 可给类方法传递参数 类的方法 (Method) 如同盒子 ( 类实例 ) 上的按钮, 当按钮被按下 ( 调用这个方法 ) 就盒子产生某种动作
类的函数成员 ( 方法 ) 类的方法 (Method) 如同盒子 ( 类实例 ) 上的按钮, 当按钮被按下 ( 调用这个方法 ) 就盒子产生某种动作
类的函数成员 ( 方法 ) 类的方法 (Method) 如同盒子 ( 类实例 ) 上的按钮, 当按钮被按下 ( 调用这个方法 ) 就盒子产生某种动作 哪一个盒子?
类的函数成员 ( 方法 ) 类的方法 (Method) 如同盒子 ( 类实例 ) 上的按钮, 当按钮被按下 ( 调用这个方法 ) 就盒子产生某种动作 哪一个按钮?
数据成员可以被方法访问
数据成员的方法也可以被调用
类方法的原型和实现的分离 如何函数的原型和实现可以分开一样, 类的函数成员 ( 方法 ) 与其原型也可以分离.h 头文件是函数原型说明,.cpp 文件是方法的实现
许多初始化是乏味的, 能否在定义类的实例时就自动进行初始化?
构造函数 (Constructors) 在类实例创建时自动调用的方法
可以接受参数 构造函数 (Constructors)
构造函数 (Constructors) 可以有多个构造函数
构造函数 (Constructors) 默认构造函数 : 无参数或参数有默认值的构造函数 class Point{ double x,y; Point(double nx = 0, double ny = 0) { x= nx; y= ny; int main(){ Point P, Q(1.2), R(2.6, 3);
构造函数 (Constructors) 一个类实例给另一个复制时, 将复制其每个成员 ( 调用默认拷贝构造函数 (default copy constructor))
构造函数 (Constructors) 你可以定义自己的拷贝构造函数
构造函数 (Constructors) 为何定义自己的拷贝构造函数? 默认的拷贝构造函数硬拷贝每个成员可能不是我们需要的! class Student { char *name ; int stuid; Student(){ name = ; stuid = 0; void main( ){ Student stu1; stu1.stuid = 98; char n[] = "foo"; stu1.name = n; Student stu2 = stu1; stu2.name[0] = 'b'; cout << stu1.name; //boo 对 stu2 的修改引起了 stu1 的改变!
构造函数 (Constructors) 为何定义自己的拷贝构造函数? 默认的拷贝构造函数硬拷贝每个成员可能不是我们需要的! class Student { char *name ; int stuid; Student(){ name = ; stuid = 0; Student(Student &o){ name = strdup(o.name); stuid = o. stuid ; void main( ){ Student stu1; stu1.stuid = 98; char n[] = "foo"; stu1.name = n; Student stu2 = stu1; stu2.name[0] = 'b'; cout << stu1.name; //foo 对 stu2 的修改不会改变 stu1!
初始化成员列表 构造函数 (Constructors) class Rectangle { int width,height; Rectangle(int,int); int area() {return width*height; Rectangle::Rectangle (int x, int y) { width=x; height=y;
初始化成员列表 构造函数 (Constructors) class Rectangle { int width,height; Rectangle(int,int); int area() {return width*height; Rectangle::Rectangle (int x, int y) : width(x) { height=y;
初始化成员列表 构造函数 (Constructors) class Rectangle { int width,height; Rectangle(int,int); int area() {return width*height; Rectangle::Rectangle (int x, int y) : width(x), height(y) {
构造函数 (Constructors) 对于没有默认构造函数的类成员, 该成员必须在初始化成员列表中初始化! class Circle { double radius; Circle(double r) : radius(r) { double area() {return radius*radius*3.14159265; class Cylinder { Circle base; double height; Cylinder(double r, double h) : base (r), height(h) { double volume() { return base.area() * height;
#include <iostream> int main () { Cylinder foo (10,20); std:: cout << "foo's volume: " << foo.volume() << '\n'; return 0;
访问修饰符 (Access Modifiers) 定义类的属性能被? 访问
访问修饰符 (Access Modifiers) public : 能从任何地方访问
访问修饰符 (Access Modifiers) private : 只能从类内部的方法访问
访问修饰符 (Access Modifiers) getters: 提供对私有数据成员的查询方法
默认的访问修饰符 (Default Access Modifiers) class: 默认是 private
默认的访问修饰符 (Default Access Modifiers) struct 默认是 public struct 是对 C 中的 struct 的扩展 ( 增加了方法 ) struct 和 class 都是定义类类型, 除了其默认访问修饰符不同!
默认的访问修饰符 (Default Access Modifiers) struct: 默认是 public class: 默认是 private
作业 定义一个表示三维向量类 Vector3, 能够完成常见的数学向量运算, 如加减 点积 差积 求长等