第2章 Class

news/2024/10/19 19:36:45/

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,不是成员变量xy = y;
}
//正确版本
void Point::init(int x, int y){this -> x = x;//这个不能把参数x赋值给成员变量x,这里的x都是局部变量x,不是成员变量xthis -> y = y;
}

:: —— resolver(域解析器)

<Class Name>::<function name>
::<function name>void S::f(){::f();//Would be recursive otherwise!//如果::前没有任何东西,表明全局,调用的是全局的f()//如果没有f(),就是递归调用自己::a++;//Select the global a,全局变量aa--;//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的ireturn 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、分为无参构造函数和有参构造函数


http://www.ppmy.cn/news/96671.html

相关文章

使用Python绘制M2货币供应率曲线

M2广义货币供应量&#xff1a;流通于银行体系之外的现金加上企业存款、居民储蓄存款以及其他存款&#xff0c;它包括了一切可能成为现实购买力的货币形式&#xff0c;通常反映的是社会总需求变化和未来通胀的压力状态。近年来&#xff0c;很多国家都把M2作为货币供应量的调控目…

Go语言的命令

常用命令 假如你已安装了golang环境&#xff0c;你可以在命令行执行go命令查看相关的Go语言命令&#xff1a; Go语言是一门编译型语言&#xff0c;通过命令行工具来编译、运行和管理代码。以下是Go语言的一些常用命令及其用法&#xff1a; go run&#xff1a;用于编译并直接…

pycharm环境下打开Django内置的数据库Sqlite出错问题解决

问题描述 在数据库库文件中写入一条记录后&#xff0c;在pycharm的terminal终端下执行查看表的命令出错 执行语句为&#xff1a; 连接数据库报错 python manage.py dbshell CommandError: You appear not to have the sqlite3 program installed or on your path. Error:…

学习笔记——vue中使用el-dropdown组件报错

今天在工作中&#xff0c;发现使用el-select做的下拉框&#xff0c;下拉菜单展开后&#xff0c;鼠标点击下拉框之外的区域时&#xff0c;下拉菜单没有收起。然后&#xff0c;我打开控制台&#xff0c;发现了这个错误。 Uncaught TypeError: Cannot read properties of null (re…

Spring注解

什么是基于Java的Spring注解配置? 给一些注解的例子 基于Java的配置&#xff0c;允许你在少量的Java注解的帮助下&#xff0c;进行你的大部分Spring配置而非通过XML文件。 以Configuration 注解为例&#xff0c;它用来标记类可以当做一个bean的定义&#xff0c;被Spring IOC容…

SAP MM 根据采购订单反查采购申请

如何通过采购订单号查询到其前端的采购申请号。 首先从采购申请的相关报表着手&#xff0c;比如ME5A, 发现它是可以满足需求的。 例如&#xff1a;如下的采购订单&#xff0c; 该订单是由采购申请10003364转过来的。 如果想通过这个采购订单找到对应的采购申请&#xff0c;在…

Arduino+ESP8266 MCU开发板 ----带你开发DHT11温湿度开发项目

目录 PC调试过程如图 手机APP可在各大商场APP中下载 手机APP调试结果/效果如图 ESP8266 MUC介绍 ESP8266 MUC主要特点&#xff1a; 步1&#xff1a;下载Arduino&#xff0c;本次不多做说明&#xff0c;本次使用的arduino软件为老版本的&#xff0c;新版本有关的问题本人…

SAP-MM-采购申请审批那些事!

1、ME55不能审批删除行项目的PR 采购申请审批可以设置行项目审批或抬头审批。如果设置为抬头审批时&#xff0c;ME55集中审批时&#xff0c;就会发现有些采购申请时不能审批的&#xff0c; 那么这些采购申请时真的不需要审批么&#xff1f;不是的&#xff0c;经过核对这些采购申…