【C++】指针的引用及面向对象
指针的引用
#include <iostream> using namespace std;struct Teacher {char name[64];int age; };int getTeacher(Teacher **p) {Teacher *tmp = NULL;if (p == NULL) {return -1;}tmp = (Teacher *)malloc(sizeof(Teacher));if (tmp == NULL) {return -2;}tmp->age = 33;*p = tmp; }int getTeacher2(Teacher* &myp) {myp = (Teacher *)malloc(sizeof(Teacher));if (myp == NULL) {return -1;}myp->age = 36; }void FreeTeacher(Teacher *pT1) {if (pT1 == NULL) {return;}free(pT1); }void main() {Teacher *pT1 = NULL;getTeacher(&pT1);cout << "age:" << pT1->age << endl;FreeTeacher(pT1);getTeacher2(pT1);cout << "age:" << pT1->age << endl;FreeTeacher(pT1);cout << "hello..." << endl;system("pause"); }
在C++中可以声明const引用
const Type& name = var;
const引用让变量拥有只读属性
const引用总结
1.const& int e 相当于const int * const e
2.普通引用相当于int *const e1
3.当使用常量(字面量)对const引用进行初始化时,C++编译器会为常量值分配空间,并将引用名作为这段空间的别名
4.使用字面量对const引用初始化后,将生成一个只读变量
inline void printA() {int a = 10;cout << "a" << a << endl; }
编辑器将内联函数直接插入函数调用的地方,一般是常用的小函数
内联函数中不能写循环语句
结论:
1)内联函数在编译时直接将函数体插入函数调用的地方
2)inline只是一种请求,编译器不一定允许这种请求
3)内联函数省去了普通函数调用时压栈,跳转和返回的开销
#include <iostream> using namespace std;inline int myfunc(int a, int b) {return a < b ? a : b; }#define MYFUNC(a,b)((a)<(b)?(a):(b))void main() {int a = 1;int b = 3;//int c = myfunc(++a, b); 输出结果:2;3;2int c = MYFUNC(++a, b); //宏替换并展开 ((++a) < (b) ? (++a) : (b)) 输出结果:3;3;3cout << "a:" << a << ";b:" << b << ";c:" << c << endl;system("pause"); }
默认参数
#include <iostream> using namespace std;void myPrint(int x = 3) {cout << "x:" << x << endl; }void myPrint2(int x = 3, int y = 4) {cout << "x:" << endl; }void main() {//myPrint();//myPrint(4);myPrint2(4);system("pause"); }
函数默认参数的规则
只有参数列表后面部分的参数才可以提供默认参数值
一旦在一个函数调用中开始使用默认参数值,那么这个参数后的所有参数都必须使用默认参数值
函数占位参数
#include <iostream> using namespace std;void func(int a, int b, int) {cout << "a:" << a << "b:" << b << endl; }void main() {//func(1, 2); 两个参数不适用func(1, 2, 3);
当函数默认参数遇上函数重载会发生什么
int func(int a, int b, int c = 0) {return a * b * c; }int func(int a, int b) {return a + b; }
存在二义性,编译不通过
//函数指针 基础的语法 //1声明一个函数类型 typedef void (myTypeFunc)(int a,int b) ; //int //myTypeFunc *myfuncp = NULL; //定义一个函数指针 这个指针指向函数的入口地址//声明一个函数指针类型 typedef void (*myPTypeFunc)(int a,int b) ; //声明了一个指针的数据类型 //myPTypeFunc fp = NULL; //通过 函数指针类型 定义了 一个函数指针 ,//定义一个函数指针 变量 void (*myVarPFunc)(int a, int b); // myPTypeFunc fp; //定义了一个 函数指针 变量
封装
#include <iostream> using namespace std; class MyCircle { public:double m_r;double m_s; public:double getR() {return m_r;}void setR(double r) {m_r = r;}double getS() {m_s = 3.14*m_r*m_r;return m_s;} };void printCircle01(MyCircle *pC) {cout << "r:" << pC->getR() << endl;cout << "s:" << pC->getS() << endl; }void printCircle02(MyCircle &myc) {cout << myc.getS() << endl; }void printCircle03(MyCircle myc) {}void main() {MyCircle c1, c2;c1.setR(10);cout << "c1 s:" << c1.getS() << endl;c1.setR(11);printCircle01(&c1);c2.setR(20);printCircle01(&c2);printCircle02(c2);system("pause"); }
lclass成员变量默认是私有的
struct成员变量默认是共有的
#pragma once //只包含一次相当于#ifndef __MYTEACHER_H_ //ctrl +shift + u 变大写 #define __MYTEACHER_H_ ... #endif
这样.h文件就只会写一次到类文件中去
练习1:判断两个立方体是否相同
Cube.h
#pragma once class Cube { public:void setA(int a);void setB(int b);void setC(int c);int getA();int getB();int getC();void setABC(int a, int b, int c); private:int m_a;int m_b;int m_c;int m_s;int m_v; public:int judgeCube(Cube &v2); };
Cube.cpp
#include "Cube.h"void Cube::setA(int a) {m_a = a; }void Cube::setB(int b) {m_b = b; } void Cube::setC(int c) {m_c = c; } int Cube::getA() {return m_a; } int Cube::getB() {return m_b; } int Cube::getC() {return m_c; }void Cube::setABC(int a, int b, int c) {m_a = a; m_b = b; m_c = c; }int Cube::judgeCube(Cube &v2) {if (m_a == v2.getA() && m_b == v2.getB() && m_c == v2.getC()) {return 1;}else {return 0;} }
main.cpp
#include <iostream> using namespace std; #include "Cube.h"void main() {Cube v1, v2;v1.setABC(1, 2, 3);v2.setABC(2, 3, 4);cout << v1.judgeCube(v2)<< endl;system("pause"); }
练习2:判断点是否在圆内
Point.h
#pragma once class Point { public:int getX();int getY();void setPoint(int _x, int _y); private:int x;int y; };
Point.cpp
#include "Point.h"void Point::setPoint( int _x, int _y) {x = _x; y = _y; } int Point::getX() {return x; } int Point::getY() {return y; }
Circle.h
#pragma once #include "Point.h" class Circle { public:void setCircle(int _r, int _x, int _y);int judge(Point &p); private:int r;int x;int y; };
Circle.cpp
#include "Circle.h" #include "Point.h"void Circle::setCircle(int _r, int _x, int _y) {r = _r; x = _x; y = _y; } int Circle::judge(Point &p) {int dd =(x - p.getX())*(x - p.getX()) + (y - p.getY())*(y - p.getY());if (dd <= r*r) {return 0;}else {return 1;} }
main.cpp
#include <iostream> using namespace std; #include "Circle.h" #include "Point.h"void main() {Circle c;Point p;c.setCircle(2, 3, 3);p.setPoint(4, 4);if (c.judge(p) == 0) {cout << "点在圆内" << endl;}else {cout << "点在圆外" << endl;}system("pause"); }
转载于:https://www.cnblogs.com/anni-qianqian/p/7196820.html
【C++】指针的引用及面向对象相关推荐
- C++ 中为什么要有“ 指针” 和 “ 引用”
推荐阅读 1.<C++语言的设计与演化> 2. <<more effective C++>> 1. 引言 1.1 对象: C++primer中, 对象的定义: 对象 ...
- C++指针与引用的区别
(1)指针是一个变量,本身占有内存,内存中存储的是所指向对象的地址.引用是内存的别名. (2)指针可以通过解引用的方式,取出所指向内存中的值.引用没有解引用. (3)指针可以先定义,然后在后面初始化. ...
- C++:const的使用(普通常量、指针、引用)
const声明的是常量,常量基本上只能读不能写.其实x也是能写的,但他只是在x初始化的时候完成了写操作. int main() {const int x=4;//x是const int型.在初始化时写 ...
- 指针,引用之间的关系
在这之前不清楚引用,和指针有怎样的关系,但今天一个例子让我们知道了指针和引用在程序中怎样运用 在这个地方我们是初始化初始化一个顺序表, 我们在主函数中是定义一个结构体的变量: SqList L; 然后 ...
- C++ demo:文本搜索以及'指针的引用'的思考
功能: 本程序允许用户在一个给定的文件中查询单词.查询结果是单词在文件中出现的次数及其所在行的列表.如果一个单词在一行中出现多次,此行只列出一次.行会按照升序输出,即第7行会在第9行之前显示. 思路: ...
- C++的常量、指针、引用
一.C语言的常量是可以通过地址进行修改的:而C++的常量是不可修改的,且在定义的时候必须初始化! 可引用常量的地址,但只是临时地址: const int a = 10 ; int *b = (int* ...
- C++编程进阶4(C++中的强制类型转换运算符、不要返回自定义类内部成员的指针,引用和迭代器)
十五.C++中的强制类型转换 C语言中的强制类型转换方式有两种 T i = (T)exp; T i = T(exp); 这两种方式没有差别,C++中也支持这两种方式,但是和C++中的强制类型转换运算符 ...
- C++知识点2——指针、引用基础
引用: 1.引用就是对象或变量的别名,但引用不是对象或变量 int a=10; int &b=a; 这里面,b就是变量a的另一个名字,对b操作就是对a操作,对a操作,b也会变化 void te ...
- c++值传递,指针传递,引用传递以及指针与引用的区别
值传递: 形参是实参的拷贝,改变形参的值并不会影响外部实参的值.从被调用函数的角度来说,值传递是单向的(实参->形参),参数的值只能传入, 不能传出.当函数内部需要修改参数,并且不希望这个改变影 ...
- 【C++ 语言】引用 ( 引用简介 | 指针常量 | 常量指针 | 常引用 | 引用参数 | 引用 指针 对比 )
文章目录 I . 引用概念 II . 引用声明 III . 引用 地址 内存 分析 IV . 常引用 V . 引用作为参数 VI . 引用 与 指针 对比 I . 引用概念 C++ 对 C 扩充 : ...
最新文章
- PostgreSQL Oracle 兼容性之 - PL/SQL FORALL, BULK COLLECT
- netstat/nmap/netcat用法
- Coroutine in Java - Quasar Fiber实现--转载
- 面向对象编程之生成器与迭代器
- boost::signals2::deconstruct相关的测试程序
- php 数据显示,数据显示处理,该怎么处理
- 数据说话,88000条数据绘制北京市地图
- android上运行java命令,Java exec()不在android上运行busybox命令
- 怎么用计算机改变声音的音调,调音台使用教程大全
- EF调用执行Oracle中序列
- swift中的category,扩展
- Unity3d 技巧(9) 游戏上线必备 适配Steam 安装包需要用到的资料
- 淘宝开放平台深入浅出
- ubuntu下dbus工具d-feet不显示路径和接口
- 跨境电商如何制定社交媒体营销策略?
- 《机器学习入门实战》第 01 篇 如何入门机器学习?
- 修改windows的主机映射文件(hosts文件)
- Java链表的书_JAVA实现双向链表的增删功能,完整代码package linked;class LinkedTable{ }public class Linke...
- 路由器能赚钱 这事靠谱吗
- Ken按特性开发核心思想
热门文章
- mysql_fdw在windows_Citus是否支持使用mysql_fdw创建分片?
- maya前台渲染_maya前台渲染MEL
- python切面_Python装饰器与面向切面编程
- java 解析 xml中的冒号_Java jdom解析xml文件带冒号的属性
- 太极发送卡片软件_太极APP的入门级使用方法
- 基于Python3爬取百度百科内容(可自己输入关键字)
- 网 络 响 应 状 态 码 常 见 的 错 误 代 码 及 错 误 原 因
- log4net配置mysql_使用独立的log4net.config文件配置log4net,将日志记录到Mysql数据库【原创】...
- mysql的空白值mac,Mac下mysql安装启动遇到的坑,及数据库常用指令
- cocos2d 屏幕適配_cocos2dx 3.2 屏幕适配的理解