C++学习笔记(2)——类与对象

回顾:

1.函数的缺省参数

​ 1.1 声明的时候写,定义的时候不写
​ 1.2 从右往左
​ 1.3 核函数重载同用时需注意二义性问题

2.函数重载

​ 2.1 在同一作用域内,函数名相同,参数列表不通
​ 2.2 参数列表不同的方式
​ 2.2.1 参数个数不同
​ 2.2.2 参数顺序不同
​ 2.2.3 参数类型不同

  1. vs编译器的编程框架

    stdafx.h的使用:
    ​ 所有的.h头文件在其中引用

    项目中所有的.cpp问题必须包含stdafx.h

  2. 命令行参数

    ​ argc:命令行字符串的个数
    ​ argv:命令行的所有字符串(本质是一个二维数组)
    ​ argv[0] 字符串
    ​ argv[0][1] 字符

  3. 字符集

    1. 多字节字符集 strcmp strcpy strcat char
    2. 宽字节字符集 wchar_t

类与对象:

​ C++中 结构体中可以有成员函数
​ C++中 使用结构体的时候可以省略关键字

// 类和对象.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include<string.h>
/*
编程语言描述的有且只有两种:
(C)事:流程 if else 循环 goto while for continue break
(C++)物:变量  数组  结构体

万物皆对象!!(先有类,再有对象)
类:概念	类型
对象:具体的实例
*/
struct student {//定义类型	类
				//成员变量
	char name[20];
	int age;
	double score;
	//成员函数(C++)	功能	事  属性
	//比如 show自己
	void show() {
		printf("名字:%s\n", name);
		printf("年龄:%d\n", age);
		printf("成绩:%g\n", score);
	}
	void Init(char* name, int age, double score) {
		strcpy(this -> name,name);
		this -> age = age;
		this -> score = score;
	}
};

	//C语言中
void show(struct student* p) {
	printf("名字:%s\n", p->name);
	printf("年龄:%d\n", p->age);
	printf("成绩:%g\n", p->score);
}

int main()
{
	//结构   构造出	结构体		对象
	struct student s1, s2, s3;
	strcpy(s1.name, "zhangsan");
	s1.age = 66;
	s1.score = 90.05;

	show(&s1);
	s1.show();
	s2.Init("lisi",19,70.50);
	show(&s2);
	s2.show();

	while (1);
	return 0;
}

名字空间:

​ 为什么会有名字空间?
​ 一个项目 很多人 一起开发
​ 标识符 重用
​ fun1

​ 名字空间的使用:
​ 1.自定义名字空间

namespace name1{
    语句;
}

​ 2.使用名字空间

	2.1 using namespace name1;
		使用整个名字空间中的名字,而且使用了就全部暴露了
    2.2 临时使用名字空间中的名字
         名字空间名::标识符
         例如:std::cout std::endl std::cin
    2.3 使用名字空间中的某个名字(类似python调用库函数)
         using 名字空间名::某名字
//名字空间第一种方法
#include "stdafx.h"
#include<string.h>
#include<iostream>
using namespace std;
namespace name1 {//名字空间的名字 name1
	int n = 10;
	void fun1() {
		printf("名字空间1的fun1函数\n");
	}
}
namespace name2 {
	int n = 20;
	void fun1() {
		printf("名字空间2的fun1函数\n");
	}
}
int main() {
	using namespace name1;
	printf("%d \n",n);
	fun1();
	printf("%d \n", name2::n);
	name2::fun1();  
	cout << "我最帅\n";
    
	while (1);
	return 0;
}

C++的头文件:

iostream	#include\<iostream>		iostream.h
    在C++代码中引用C语言自带的库
    	方式1:c风格	#include<string.h>
    	方式2:C++风格 #include<cstring>
//名字空间第二种方法
#include<string>	//string类
#include<string.h>	//等同于#include<cstring>,strcmp函数的头文件
#include<iostream>
int main() {
	//using namespace std;
	//C++风格的字符串
	std::string str = "你好";
	std::string str1;
	str1 = str;
	//C风格字符串
	printf("str:%s\n", str.c_str());
	printf("str:%s\n", str1.c_str());
	printf("strsize:%d\n", str.size());
	str1 += str;
	printf("str:%s\n", str1.c_str());
	while (1);
	return 0;
}
//名字空间第三种方法
#include<string>	//string类
#include<string.h>	//等同于#include<cstring>,strcmp函数的头文件
#include<iostream>
using std::string;
int main() {
	//C++风格的字符串
	string str = "你好";
	string str1;
	str1 = str;
	//C风格字符串
	printf("str:%s\n", str.c_str());
	printf("str:%s\n", str1.c_str());
	printf("strsize:%d\n", str.size());
	str1 += str;
	printf("str:%s\n", str1.c_str());
	while (1);
	return 0;
}

C++的动态内存分配:

参考:【C++】动态内存分配详解(new/new[]和delete/delete[])_C/C++_Civil跨界工程师-CSDN博客 https://blog.csdn.net/qq_40416052/article/details/82493916

​ C语言基础之上能够做优化
​ malloc 和 free 函数
​ C++的动态内存分配
​ new 和 delete 运算符

newmalloc
new关键字是C++的一部分malloc是由C库提供的函数
new以具体类型为单位进行内存分配malloc以字节为单位进行内存分配
new在申请单个类型变量时可进行初始化malloc不具备内存初始化的特性
#include"stdafx.h"
#include<iostream>
using namespace std;
#include<stdlib.h>
int main(){
    int n;
    int* pc = (int*)malloc(sizeof(int));
    int* p = new int;
    int* pa = new int[6];
    cin >> n;	//scanf("%d",&n);
    *p = n;
    cout << "n:" << *p  << end1;//printf("n:%d\n",*p);
    
    while(1);
    return 0;
}

内联函数,this指针:

​ this是个指针变量:

​ 1.每个对象都有这个指针变量
​ 2.这个指针变量指向对象本身

​ 内联函数:

​ inline 关键字声明一个函数为内联函数
​ 声明函数的同时定义函数(建议编译器将此函数设置为内联函数)
​ 以膨胀内存的方式来节约时间

​ 函数调用过程:

​ 定义——声明——调用(需要跳转时间,参数出栈入栈)
​ 两种解决方案:
1.宏函数 #define sum(x,y) ((x)+(y)) 只做简单代码替换
2.内联函数

​ 两者区别:
​ 1.时机
​ 宏函数:预处理
​ 内联函数:编译
​ 2.编译器是否优化
​ 宏函数:编译器不会优化,不做语法检查
​ 内联函数:编译器会优化,做语法检查
​ 3.传参
​ 宏函数无传参过程,内联函数有传参过程

​ 什么情况不建议设置为内联:
​ 代码行数超过五行;
​ 有循环递归等复杂流程;
​ 调用次数少。

#pragma once //防止多次引用
struct Mystring {
	//成员变量
	char* buff;//字符串
	size_t size;//字符个数
				//成员函数
	void init();
	void strSet(char* str);
	inline size_t getSize();//内联函数声明
	void show();
};
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO:  在此处引用程序需要的其他头文件
#include"Mystring.h"
#include<string.h>
#include<stdlib.h>
//Mystring.cpp
#include "stdafx.h"

void Mystring::init() {
	buff = NULL;
	size = 0;
	printf("初始化!\n");
}
void Mystring::strSet(char* str) {
	//buff = str;
	//1.获取字符串长度
	size = strlen(str);
	//2.开辟内存
	if (buff)//如果原内存存在,释放
		free(buff);
	buff = (char*)malloc(size + 1);
	memset(buff, 0, size + 1);//每个字节置为'\0'
	//3.保存所有字符
	//for (int i = 0; i < size; i++) buff[i] = str[i];
	//syrcpy(buff,str);	等同	只针对字符串,一个一个
	memcpy(buff,str,size);	//速度更快	针对内存段,整块
}
size_t Mystring::getSize() {
	return size;
}
void Mystring::show() {
	printf("buff: %s\n",buff);
}
//main.cpp
#include "stdafx.h"//.cpp文件中都要引用
int main()
{
	Mystring str1;
	str1.init();
	str1.strSet("hahahah");
	printf("size: %d\n", str1.getSize());
	str1.show();
	while (1);
	return 0;
}

C语言实现面向对象:

#include <stdio.h>

void init(struct student* s, int a, double d);
void show(struct student* s);
//void ()(struct student* , int , double )	//函数类型	
struct student {
	//成员变量
	int age;
	double score;
	struct student* MyThis;
	//成员函数(使用函数指针)
	void(*pFuncInit)(struct student*, int, double);//指针变量名
	void(*pFuncShow)(struct student*);
};
int main() {
	struct student s1;
	s1.MyThis = &s1;
	s1.pFuncInit = init;	//给函数指针变量赋值
	s1.pFuncShow = show;

	s1.pFuncInit(&s1, 33, 88.66);
	s1.pFuncShow(&s1);

	while (1);
	return 0;
}

void init(struct student* s, int a, double d) {
	//s->age = a;
	s->MyThis->age = a;
	//s->score = d;
	s->MyThis->score = d;
}
void show(struct student* s) {
	printf("age:%d score:%g\n",s->age,s->score);
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值