【C++】指针的引用及面向对象
指针的引用
#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++】指针的引用及面向对象的更多相关文章
- [速记]关于指针,引用和递归和解递归——C++
在写基于二叉排序树的查找时,分为三个过程 1.二叉排序树的插入 2.二叉排序树的建立 3.基于二叉排序树的查找 其中第三部可以递归方式实现,也可以用while循环解递归,于是我想也解解第一步的递归,看 ...
- C++指针参数引用
粘个代码占位置,以后有时间把指针函数,函数指针都补上 #include <iostream> using namespace std; void freePtr1(int* p1){ /* ...
- C/C++:提升_指针的指针和指针的引用
C/C++:提升_指针的指针和指针的引用 写在前面 今天在使用指针的时候我发现了一个自己的错误.
- C++_系列自学课程_第_8_课_指针和引用_《C++ Primer 第四版》
C语言最富有迷幻色彩的部分当属指针部分,无论是指针的定义还是指针的意义都可算是C语言中最复杂的内容.指针不但提供给了程序员直接操作硬件部分的操作接口,还提供给了程序员更多灵活的用法.C++继承这一高效 ...
- C++学习笔记 指针与引用
指针与引用 1. 指针 (1) 指针是一个变量(实体),存储的是一个地址,指向内存的一个存储单元,指针可以为空 (2) 指针可以为空,在声明定义时可以不初始化 (3) 指针在初始化之后可以重新指向其 ...
- 数组类型与sizeof与指针的引用
以char类型为例: char a[100]; //a类型为char[100] &a类型为 char (*)[100] *a类型为char char *p = a; ...
- c++指针与引用问题
本来是回答问题的,到这里做个笔记 *&L是指针的引用,实参是个指针.所以L是实参指针的别名,对别名L的修改,等于对实参的修改.*L是传值,你无法改变传过来的实参指针变量的值程序代码: #inc ...
- C++ 中指针与引用的区别
指向不同类型的指针的区别在于指针类型可以知道编译器解释某个特定地址(指针指向的地址)中的内存内容及大小,而void*指针则只表示一个内存地址,编译器不能通过该指针所指向对象的类型和大小,因此想要通过v ...
- 详解c++指针的指针和指针的引用
展示一下使用指针的指针和指针的引用修改传递给方法的指针,以便更好的使用它.(这里说的指针的指针不是一个二维数组) 为什么需要使用它们 当我们把一个指针做为参数传一个方法时,其实是把指针的复本传递给了方 ...
随机推荐
- 《深入Java虚拟机》笔记
当运行一个Java程序的同时,也就在运行了一个Java虚拟机实例.Java虚拟机实例通过调用某个初始类的mian()方法来运行一个Java程序运行中Java程序的每一个线程都是一个独立的虚拟机执行引擎 ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 洛谷 2186 小Z的栈函数
https://www.luogu.org/problem/show?pid=2186 题目描述 小Z最近发现了一个神奇的机器,这个机器的所有操作都是通过维护一个栈来完成的,它支持如下11个操作: N ...
- poj 3636
Nested Dolls http://poj.org/problem?id=3636 Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- 【BZOJ】2337: [HNOI2011]XOR和路径 期望+高斯消元
[题意]给定n个点m条边的带边权无向连通图(有重边和自环),在每个点随机向周围走一步,求1到n的期望路径异或值.n<=100,wi<=10^9. [算法]期望+高斯消元 [题解]首先异或不 ...
- Daily Report-1126
今日: 上午主要是回顾了react,阅读官方文档的时候发现了list中key值设计的必要性. 看了部分react源码,发现有些吃力,在询问羽牧学长之后调整策略,从redux和mobx入手,先多熟悉用法 ...
- 蓝色的cms网站后台管理模板——后台
链接:http://pan.baidu.com/s/1c138cwC 密码:9vy9
- Hive ORC表的使用
创建普通临时表: create table if not exists test_orc_tmp( name string, gender string, cnt BIGINT )row ...
- 33 - 并发编程-线程同步-Event-lock
目录 1 线程同步 1.1 Event 1.1.1 什么是Flag? 1.1.2 Event原理 1.1.3 吃包子 1.2 Lock 1.2.1 lock方法 1.2.2 计数器 1.2.3 非阻塞 ...
- 20行js代码制作网页刮刮乐
分享一段用canvas和JS制作刮刮乐的代码,JS部分去掉注释不到20行代码效果如下 盖伦.jpg 刮刮乐.gif HTML部分 <body>  &l ...