指针的引用

#include <iostream>
using namespace std; struct Teacher
{
char name[];
int age;
}; int getTeacher(Teacher **p) {
Teacher *tmp = NULL;
if (p == NULL) {
return -;
}
tmp = (Teacher *)malloc(sizeof(Teacher));
if (tmp == NULL) {
return -;
}
tmp->age = ;
*p = tmp;
} int getTeacher2(Teacher* &myp) {
myp = (Teacher *)malloc(sizeof(Teacher));
if (myp == NULL) {
return -;
}
myp->age = ;
} 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 = ;
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 = ;
int b = ;
//int c = myfunc(++a, b); 输出结果:2;3;2
int c = MYFUNC(++a, b); //宏替换并展开 ((++a) < (b) ? (++a) : (b)) 输出结果:3;3;3
cout << "a:" << a << ";b:" << b << ";c:" << c << endl;
system("pause");
}

默认参数

#include <iostream>
using namespace std; void myPrint(int x = ) {
cout << "x:" << x << endl;
} void myPrint2(int x = , int y = ) {
cout << "x:" << endl;
} void main() {
//myPrint();
//myPrint(4);
myPrint2();
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(, , );

当函数默认参数遇上函数重载会发生什么

int func(int a, int b, int c = )
{
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();
cout << "c1 s:" << c1.getS() << endl; c1.setR();
printCircle01(&c1); c2.setR();
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 ;
}
else {
return ;
}
}

main.cpp

#include <iostream>
using namespace std;
#include "Cube.h" void main() {
Cube v1, v2;
v1.setABC(, , );
v2.setABC(, , );
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 ;
}
else {
return ;
}
}

main.cpp

#include <iostream>
using namespace std;
#include "Circle.h"
#include "Point.h" void main() {
Circle c;
Point p;
c.setCircle(, , );
p.setPoint(, );
if (c.judge(p) == ) {
cout << "点在圆内" << endl;
}
else {
cout << "点在圆外" << endl;
}
system("pause");
}

【C++】指针的引用及面向对象的更多相关文章

  1. [速记]关于指针,引用和递归和解递归——C++

    在写基于二叉排序树的查找时,分为三个过程 1.二叉排序树的插入 2.二叉排序树的建立 3.基于二叉排序树的查找 其中第三部可以递归方式实现,也可以用while循环解递归,于是我想也解解第一步的递归,看 ...

  2. C++指针参数引用

    粘个代码占位置,以后有时间把指针函数,函数指针都补上 #include <iostream> using namespace std; void freePtr1(int* p1){ /* ...

  3. C/C++:提升_指针的指针和指针的引用

    C/C++:提升_指针的指针和指针的引用 写在前面 今天在使用指针的时候我发现了一个自己的错误.

  4. C++_系列自学课程_第_8_课_指针和引用_《C++ Primer 第四版》

    C语言最富有迷幻色彩的部分当属指针部分,无论是指针的定义还是指针的意义都可算是C语言中最复杂的内容.指针不但提供给了程序员直接操作硬件部分的操作接口,还提供给了程序员更多灵活的用法.C++继承这一高效 ...

  5. C++学习笔记 指针与引用

    指针与引用  1. 指针 (1) 指针是一个变量(实体),存储的是一个地址,指向内存的一个存储单元,指针可以为空 (2) 指针可以为空,在声明定义时可以不初始化 (3) 指针在初始化之后可以重新指向其 ...

  6. 数组类型与sizeof与指针的引用

    以char类型为例: char a[100];     //a类型为char[100]    &a类型为 char (*)[100]    *a类型为char char *p = a;     ...

  7. c++指针与引用问题

    本来是回答问题的,到这里做个笔记 *&L是指针的引用,实参是个指针.所以L是实参指针的别名,对别名L的修改,等于对实参的修改.*L是传值,你无法改变传过来的实参指针变量的值程序代码: #inc ...

  8. C++ 中指针与引用的区别

    指向不同类型的指针的区别在于指针类型可以知道编译器解释某个特定地址(指针指向的地址)中的内存内容及大小,而void*指针则只表示一个内存地址,编译器不能通过该指针所指向对象的类型和大小,因此想要通过v ...

  9. 详解c++指针的指针和指针的引用

    展示一下使用指针的指针和指针的引用修改传递给方法的指针,以便更好的使用它.(这里说的指针的指针不是一个二维数组) 为什么需要使用它们 当我们把一个指针做为参数传一个方法时,其实是把指针的复本传递给了方 ...

随机推荐

  1. [LeetCode] 23. Merge k Sorted Lists ☆☆

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  2. 从零搭建SSM框架(二)运行工程

    启动cnki-manager工程 1.需要在cnki-manager 的pom工程中,配置tomcat插件.启动的端口号,和工程名称. 在cnki-manager的pom文件中添加如下配置: < ...

  3. html css 如何将表头固定

    position属性取值为fixed时,则元素的位置将不受滚动条的影响,而是直接依据窗口定位,这就是将表头固定的最直接方法,网上其他途径感觉都是在走弯路.但是与此同时必须解决两个问题.第一:表体将随之 ...

  4. CodeForces 1011B

    Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...

  5. [SCOI2010]生成字符串 题解(卡特兰数的扩展)

    [SCOI2010]生成字符串 Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数 ...

  6. Unity MMO 参考数值

    贴图格式: iOS :RGBA 32 (pvrtc 4 ) Android : RGB Compresed ETC 4 或 RGBA 32  . DrawCall: 总计Drawcall 平均 100 ...

  7. leetcode.C.4. Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...

  8. git push multiple repo

    git remote add xxx https://git.github.com

  9. keypress 、keydown、keyup后触发回车

    1.keypress .keydown.keyup的区别 keypress表示键盘按下的全过程,只有按下任意字母数字键(后退.删除等系统功能键无效)时才触发,捕获到的keyCode区分大小写 keyd ...

  10. python基础===拆分字符串,和拼接字符串

    给定某字符,只需要保留其中的有效汉字或者字母,数字之类的.去掉特殊符号或者以某种格式进行拆分的时候,就可以采用re.split的方法.例如 ============================== ...