1设计类 抽象类
1.1class 类名{
1.2public 公共权限
1.3 设置 成员属性
1.4 设置 成员函数
1.5 }
1.6使用类 创建对象 实例化对象
1.7 类名 对象名
1.8通过对象 来设置属性 调用成员函数
1.9 类和对象 关系???
1.9.1类是对对象的抽象
1.9.2对象是对类的实例
设计圆类求周长.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
const double pi = 3.14;
// 1. 设计一个类,求圆的周长
// 周长公式 2 * pi * r
class Circle //class代表声明一个类 后面紧跟的是类的名称
{
public: //公共权限
//求圆的周长
//在类里面写函数 成员函数
double calculateZC()
{
return 2 * pi * m_R;
}
//设置半径的成员方法
//成员函数 可以修改成员属性
void setR(int r)
{
m_R = r;
}
//半径 成员属性
int m_R;
};
void test01()
{
//通过类 来创建一个圆
Circle c1; //圆 (对象)
//c1.m_R = 10; //给这个对象来进行半径的赋值
//通过成员函数 间接给圆设置半径
c1.setR(10);
//输出c1的周长
cout << "c1的周长为: " << c1.calculateZC() << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
设计学生类
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
/*
2. 设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓 名和学号
*/
class Student
{
public: //公共权限
//设置姓名
void setName(string name)
{
m_Name = name;
}
//设置学号
void setId(int id)
{
m_Id = id;
}
//打印信息
void showInfo()
{
cout << "姓名:" << m_Name << " 学号: " << m_Id << endl;
}
string m_Name; //姓名
int m_Id; //学号
};
void test01()
{
//创建一个学生 实例化 -- 通过类来创建对象的过程
Student st;
st.setName("张三");
st.setId(1);
//通过st的属性打印了 st信息
cout << "st的姓名为: " << st.m_Name << " st的学号:" << st.m_Id << endl;
//通过成员函数 来打印st的信息
st.showInfo();
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
2内联函数 解决宏缺陷问题
2.1给编译器一个建议,加上关键字,编译器不一定按照内联处理
2.2不加关键字,也许编译器还偷摸的给你加inline
2.3成员函数 默认加上关键字
2.4函数声明加了关键字,函数实现也要加inline关键字
内联函数的引出-宏函数的缺陷
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//定义一个加法
#define MyAdd(x,y) ((x)+(y))
void test01()
{
int ret = MyAdd(10, 20) *20; //预期结果 600 ((10)+(20))*20
cout << "ret = " << ret << endl;
}
#define MyCompare(a,b) ((a) < (b)) ? (a) :(b)
inline void mycompare(int a, int b)
{
int ret = a < b ? a : b;
cout << "ret ::::: " << ret << endl;
}
//1 内联函数注意事项
// 类内部的成员函数 默认前面会加inline关键字
inline void func(); //内联函数声明
inline void func() { }; //如果函数实现时候,没有加inline关键字 ,那么这个函数依然不算内联函数
void test02()
{
int a = 10;
int b = 20;
//int ret = MyCompare(++a, b); // 预期结果 11 ((++a) < (b)) ? (++a):(b)
//cout << "ret = " << ret << endl;
mycompare(++a, b);
}
//3 宏函数也没有作用域
int main(){
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
3函数默认参数
3.1参数可以有默认值
3.2如果有一个位置有了默认值,那么从这个位置开始,从左往右都必须有默认值
3.3函数声明和实现 只能有一个有默认值
4函数占位参数
4.1void func(int) 占位参数 调用时候必须要提供这个参数
4.2占位参数也可以有默认值
4.3c语言中没有默认参数 和占位参数
c语言中没有函数参数和占位参数.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//C语言中没有默认参数,C也没有 占位参数
//void func(int a , int)
//{
//
//}
int main2(){
system("pause");
return EXIT_SUCCESS;
}
04 函数的默认参数以及占位参数.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//函数的默认参数 参数后面 = ...
//函数参数注意事项,如果有一个位置有了默认参数,那么从这个位置开始,从左往后都必须有默认参数
// 传入参数 ,如果有参数,就用传入的参数,没有参数就用默认值
//如果函数声明里面有了 默认参数,那么函数实现时候必须没有
// 函数声明和实现里 只能有一个里有默认参数,不要同时都出现默认参数
void myFunc(int a = 10, int b = 10);
void myFunc(int a , int b ){}
void func( int a , int b = 10,int c = 1 )
{
cout << "a + b + c = " << a + b + c << endl;
}
void test01()
{
//func();
func(1,2);
}
//函数 占位参数
//如果有了占位参数,函数调用时候必须要提供这个参数 ,但是用不到参数
//占位参数 没有什么大用途,只有后面重载 ++符号才有一点点用
//占位参数 可以有默认值
void func2(int a , int = 1)
{
}
void test02()
{
func2(10);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
5函数重载的基本语法
5.1函数名称相同 又在同一个作用域下
5.2函数参数个数不同、类型不同、顺序不同都可以满足重载条件
5.3函数的返回值可以作为函数重载条件吗? 不可以
5.4当函数重载碰到了函数默认参数 要注意避免二义性
05 函数重载的基本语法.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//函数重载
// C++中 函数名称可以重复
// 必须在同一个作用域,函数名称相同
// 函数的参数 个数不同 或者 类型不同 或者 顺序不同
void func()
{
cout << "无参数的func" << endl;
}
void func(int a )
{
cout << "有参数的func(int a)" << endl;
}
void func(double a)
{
cout << "有参数的func(double a)" << endl;
}
void func(double a , int b)
{
cout << "有参数的func(double a ,int b)" << endl;
}
void func(int a, double b)
{
cout << "有参数的func(int a ,double b)" << endl;
}
//返回值可以作为函数重载的条件吗??? 不可以
//int func(int a, double b)
//{
// cout << "有参数的func(int a ,double b)" << endl;
// return 1;
//}
void test01()
{
//func(1.1,3);
func(1, 3.14);
}
//当函数重载 碰到了 默认参数时候,要注意避免二义性问题
void func2(int a,int b = 10)
{
}
void func2(int a)
{
}
void test02()
{
//func2(10);
}
//引用的重载版本
void func3(int &a) //引用必须要引合法的内存空间
{
cout << " int &a" << endl;
}
void func3(const int &a) //const也是可以作为重载的条件 int tmp = 10; const int &a = tmp;
{
cout << "const int &a" << endl;
}
void test03()
{
//int a = 10;
func3(10);
}
int main(){
//test01();
//test03();
system("pause");
return EXIT_SUCCESS;
}
6extern C浅析
6.1解决了C++文件中调用C语言的代码
6.2ifdef __cplusplus extern “C” {
6.3}
test.h
#pragma once
#ifdef __cplusplus //两个_下划线
extern "C" {
#endif // !__cplusplus
#include <stdio.h>
void show();
#ifdef __cplusplus //两个_下划线
}
#endif // !__cplusplus
test.c
#include "test.h"
void show()
{
printf("hello world \n");
}
06 extern C浅析.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "test.h"
//C++中想调用C语言方法
//extern "C" void show(); //show方法 按照C语言方式做连接
//解决的问题就是 在C++中调用C语言的函数
int main(){
show(); //在C++中 函数是可以发生重载的,编译器会把这个函数名称偷偷改变 _showv void
system("pause");
return EXIT_SUCCESS;
}
7C++语言的封装
7.1将属性和行为作为一个整体,来表示生活中具体的事物
7.2有访问权限
7.3class 和struct唯一区别 默认权限不同
7.3.1class默认是private
7.3.2struct 默认是public
7.4public 是类内类外都可以访问到
7.5protected 类内可以,类外不可以
7.6private 类内可以,类外不可以
07 C语言下的封装.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Person
{
char mName[64];
int mAge;
};
void PersonEat(struct Person *p)
{
printf("%s 在吃人饭 \n",p->mName);
}
void test01()
{
struct Person p1;
strcpy(p1.mName, "德玛西亚");
PersonEat(&p1);
}
struct Dog
{
char mName[64];
int mAge;
};
void DogEat(struct Dog * dog)
{
printf("%s 在吃狗粮 \n", dog->mName);
}
void test02()
{
struct Dog d;
strcpy(d.mName, "旺财");
DogEat(&d);
struct Person p1;
strcpy(p1.mName, "老王");
DogEat(&p1);
}
//C语言封装 属性和行为分开处理了 ,类型检测不够
int main(){
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
08 C++语言下的封装.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
struct Person
{
char mName[64];
int mAge;
void PersonEat()
{
cout << mName <<"吃人饭" << endl;
}
};
struct Dog
{
char mName[64];
int mAge;
void DogEat()
{
cout << mName << "吃狗粮" << endl;
}
};
//C++中的封装 严格类型转换检测, 让属性和行为 绑定到一起
// 1 属性和行为作为一个整体来表示生活中的事物
// 2 控制权限 public 公有权限 protected 保护权限 private 私有权限
void test01()
{
Person p1;
strcpy(p1.mName, "老王");
p1.PersonEat();
//p1.DogEat();
}
//struct 和class是一个意思,唯一的不同 默认权限 ,struct是public,但是class默认权限是private
class Animal
{
private:
//如果我不声明权限,默认的权限是 private
void eat(){ mAge = 100; };
int mAge;
public:
int mHeight;
protected: //保护权限 类内部可以访问 ,(当前类的子类可以访问) , 类外部不可以访问
int mWeight;
void setWeight(){ mWeight = 100; };
};
//所谓私有权限 就是私有成员(属性、函数) 在类内部可以访问,类外部不可以方法
//公共权限 ,在类内部和类外部都可以访问
void test02()
{
Animal an;
//an.eat();
//an.mAge; //私有不可以访问到
an.mHeight = 100; //公有权限在类外部可以访问到
//an.mWeight = 100; //保护权限 在类外不可访问到
}
// public 类内 类外 都可以访问
// protected 类内可以访问 类外 不可以访问 (子类可以方法)
// private 类内可以访问 类外 不可以访问 (子类不可以方法)
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
8建议将所有成员属性设置为私有
8.1自己提供公共的对外接口来进行 set或者get方法访问
09 建议成员变量设置为private.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
class Person
{
public:
//设置年龄
void setAge(int age)
{
if (age < 0 || age > 100)
{
cout << "你这个老妖精" << endl;
return;
}
m_Age = age;
}
//获取年龄 读权限
int getAge()
{
return m_Age;
}
//读姓名
string getName()
{
return m_Name;
}
//写姓名
void setName(string name)
{
m_Name = name;
}
//只写的情人
void setLover(string lover)
{
m_lover = lover;
}
private: //类外不可访问,类内可以访问
int m_Age = 0; //年龄 读写
string m_Name; //公有权限 读写
string m_lover; //情人 只写
};
void test01()
{
Person p1;
p1.setName("老王");
cout << "p1的姓名:" << p1.getName() << endl;
//年龄
p1.setAge(120);
cout << "p1的年龄:" << p1.getAge() << endl;
//情人 只能设置 外部我不告诉你
p1.setLover("仓井");
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}