第2章 Class

Point结构体

//C语言写法
typedef struct point{
  float x;
  float y;
}Point;

Point a;
a.x = 1;
a.y = 2;
//const表示p指向的对象里的值不能由p指针修改
void print(const Point* p){
  printf("%d %d\n", p -> x, p -> y);
}
print(&a);

//想实现点的移动,move(dx, dy)?
//要修改Point中的值,所以不加const
void move(Point* p, int dx, int dy){
  p -> x += dx;
  p -> y += dy;
}

Prototypes-原型

typedef struct point{
  float x;
  float y;
}Point;
void print(const Point*p);
void move(Point* p, int dx, int dy);

Usage

Point a;
Point b;
a.x = b.x = 1;
a.y = b.y = 1;
move(&a, 2, 2);
print(&a);
print(&b);
//c++写法,可以不写typedef。struct后面的是结构体的名字
//可以直接Point a;
struct Point{
  int x;
  int y;
  void print(); //声明,不产生实体,不会被编译出代码
};
//结构体内的print,成员函数
void Point::print(){
  //x,y就是成员变量,不用写成p -> x
  //printf("%d %d", x, y);
  cout << x << " " << y << endl;
}
//自由函数,struct里的print是成员函数
void print(const Point* p){
  //printf("%d %d\n", p -> x, p -> y);
	cout << p -> x << " " << p -> y << endl;
}

int main(){
  Point a;
  Point b;
  a.x = 1; a.y = 1;
  b.x = 3; b.y = 4;
  a.print();//a这个对象做了能做的动作
  b.print();//成员函数
  print(&a);//自由函数
  moce(&a, 10, 20);
  print(&a);
}
//修改Point::print()
void Point::print(){
  //x,y就是成员变量,不用写成p -> x
  //printf("%d %d", x, y);
  cout << this <<endl;
  cout << this -> x << " " << this -> y << endl;
}
/*
this是关键字,相当于指向当前对象的指针
a.print()相当于Point::print(&a),把a的地址取出,交给Point的print函数执行
在自己的参数前编译器会加上一个隐藏的参数Point* this.可以在成员函数中直接用,
编译器为自动为非本地变量和非全局变量加上this,使得编译通过
*/

C++ version

class Point{
public:
  void init(int x, int y);
  void move(int dx, int dy) const;
  void print() const;
private:
  int x;
  int y;
};
//错误版本
void Point::init(int x, int y){
  x = x;//这个不能把参数x赋值给成员变量x,这里的x都是局部变量x,不是成员变量x
  y = y;
}
//正确版本
void Point::init(int x, int y){
  this -> x = x;//这个不能把参数x赋值给成员变量x,这里的x都是局部变量x,不是成员变量x
  this -> y = y;
}

:: —— resolver(域解析器)

<Class Name>::<function name>
::<function name>

void S::f(){
	::f();//Would be recursive otherwise!
  //如果::前没有任何东西,表明全局,调用的是全局的f()
  //如果没有f(),就是递归调用自己
  ::a++;//Select the global a,全局变量a
  a--;//The a at class scope,f函数内部没有本地变量a,所以应该是对应的对象的成员变量a
}

C VS C++

//C语言版本
  typedef struct point { 
   float x; 
   float y; 
  } Point; 
  void print(const Point* p); 
  void move(Point* p,int dx, 
  int dy); 
  Point a; 
  a.x = 1; a.y = 2; 
  move(&a,2,2); 
  print(&a);
//C++版本
  class Point { 
  public: 
   void init(int x,int y); 
   void print() const; 
   void move(int dx,int dy); 
  private: 
   int x; 
   int y; 
  } ; 
  Point a; 
  a.init(1,2); 
  a.move(2,2); 
  a.print();

1、C++把函数放进了结构内部

2、C语言需要显示参数,表示指向结构的指针;C++有隐式的指针this

3、C++中可以用this表明这个函数正在操作的结构体

4、C++中调用函数时,使用对象.函数的方式

To call the functions in a class 调用函数

Point a; 
a.move(10,10); 
/*There is a relationship with the function be called and 
the variable to call it. 
The function itself knows it is doing something w/ the 
variable.
*/

this: the hidden parameter

隐藏参数

//• this is a hidden parameter for all member 
//functions, with the type of the struct 
void Point::move(int dx, int dy); 
//➔ (can be regarded as) 
void Point::move(Point* this, int dx, int dy)
//• To call the function, you must specify a variable 
//Point a; 
a.x = 0; a.y = 0;
a.move(10, 10);
//➔ (can be regarded as)
Point::move(&a, 10, 10);

this.cpp

#include <iostream>
using namespace std;

struct Stu{
  int i;
  void f();
}

void Stu::f(){
  cout << "Inside Stu::f(), this" << this << endl;
}

int mai(){
  Stu stu;
  cout << "Inside main(), &stu" << &stu << endl;
  stu.f();
  return 0;
}
/*
输出的两个都是stu的地址
*/

Integer.h

#ifndef _INTEGER_HEAD_
#define _INTEGER_HEAD_

struct Integer{
  int i;
  void init(int i);
  Integer* bigger(Integer* r);
};
#endif

Integer.cpp

#incldue "Integer.h"
//Integer::init是完整名称,所以void写在前
void Integer::init(int i){
  this -> i = i;
}
//返回值i更大的Integer的指针
Integer* Integer::bigger(Integer* r){
  if(r -> i > i){ //后一个i是this的i
    return r;
  }else{
    return this;
  }
}

Objects = Attributes + Services 对象=属性+服务

• Data: the properties or status 用数据表明属性和状态

• Operations: the functions 对象对外提供的服务,服务过程中可能数据会改变

只有通过服务才能得知data内容或对data进行操作

Objects

• In C++, an object is just a variable, and the purest 

definition(纯粹的定义) is “a region of storage”. 

• The struct variables mentioned before are just 

objects in C++.

Ticket Machine 售票机

• Ticket machines print a ticket  when a customer inserts the   correct money for their fare.

• Our ticket machines work by  customers ‘inserting’ money  into them, and then  requesting a ticket to be 

printed. A machine keeps a  running total of the amount 

of money it has collected  throughout its operation.

这是面向过程的思维方式

面向对象的思维方式

左边是数据,右边是服务

prompt提示

balance余额

Object vs. Class

• Objects (cat)

• Represent things, events, or concepts

• Respond to messages at run-time

• Classes (cat class)

• Define properties of instances

• Act like types in C++

OOP Characteristics

1.Everything is an object. 一切皆为对象

2.A program is a bunch of objects telling each  other what to do by sending messages. 程序是一堆对象,通过发送消息互相告知做什么。不是how to do。强调程序的自主性

3.Each object has its own memory made up of  other objects. 每个对象有他自己的内存,这个内存里有其他的对象 。

4.Every object has a type. 每个对象有一个类型。 

5.All objects of a particular type can receive  the same messages. 属于同一个特定类型的对象可以接收相同类型的消息——逆否也对:接收不同类型消息的对象属于不同的类型

Point::init()

class Point{
public:
	void init(int x, int y);
  void print() const;
  void move(int dx, int dy);
private:
  int x;
  int y;
};

Point a;
a.init(1, 2);
a.move(2, 2);
a.print();

构造函数

class Point{
public:
	Point();
  void print() const;
  void move(int dx, int dy);
private:
  int x;
  int y;
};
Point::Point(){
  cout << "Point!!" << endl;
  this -> x = 0;
  this -> y = 0;
}
int main(){
  Point a;
  cout << "------------" << endl;
  Point b;
  cout << &a << endl;
  a.print();
  b.print();
  a.move(2, 2);
  a.print();
  return 0;
}
//打印出了两次Point(),说明每次创建对象就会调用构造函数

有参构造函数

class Point{
public:
	Point(int x, int y);
private:
  int x;
  int y;
};
Point::Point(int x, int y){
  cout << "Point!!" << endl;
  this -> x = x;
  this -> y = y;
}
int main(){
  Point a(1, 2);
}

1、构造函数没有返回类型

2、对象被创建时调用构造函数

3、分为无参构造函数和有参构造函数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值