C++ 01

// 一个c++程序

//#include<iostream>
//using namespace std;
//int main()
//{
//    cout << "hello world" << endl;
//    return 0;
//}
// using namespace std; 声明一个命名空间
/*


 命名空间解决了多个模块间命名冲突的问题,例如c++库提供的对象都存放
  在了std这个标准名字空间中,例如cin,cout,endl
*/
// 第二种使用域限定符::

//#include<iostream>
//int main()
//{
//    std::cout << "hello world" << std::endl;
//    return 0;
//}


// 第三种使用using和域限定符一起

//#include<iostream>
//using std::cout;
//using std::endl;
//int main()
//{
//    cout << "hello world" << endl;
//    return 0;
//}

// 标准库iostream中的cin和cout 起输入输出作用
// 输出操作符: <<
// endl起换行作用 例如: cout<<"hello"<<endl<<"world"<<endl;
// 输入之前定义类型一致的变量来存放数据例如:
// int a, b;
// cin >> a >> b;


 c++ 数据类型: char int float double bool
 与c语言不同的是使用cin 和 cout 时不需要手动控制数据类型
 

//#include<iostream>
//using namespace std;
//int main()
//{
//    bool a = true;
//    bool b = false;
//    cout << a << b;
//    return 0;
//}

// 函数的重载

#include<iostream>
using namespace std;
// 方法1
int add(int a, int b) {
	cout << "方法1" << endl;
	return a + b;
}
// 方法2
double add(double a, double b) {
	cout << "方法二" << endl;
	return a + b;
}
// 方法三
double add(double a, int b) {
	cout << "方法三" << endl;
	return a + b;
}
int main()
{
	cout << add(1,2) << endl;
	return 0;
}

函数模板:

#include<iostream>
using namespace std;
// 定义函数模板
template<class T1,class T2>
T1 add(T1 x, T2 y)
{
	cout << sizeof(T1) << "," << sizeof(T2) << "\t";
	return x + y;
}
int main()
{
	cout << add(10, 20) << endl;
}

/ inline 内联函数, 解决一个函数代码不多却频繁被调用的问题
// 依然使用自定义函数,但是在编译的时候,把函数代码插入到函数调用处,免去函数调用的一系列过程,像普通顺序执行的代码一样

#include<iostream>
using namespace std;
inline int Max(int a, int b) {
	return a > b ? a : b;
}
int main()
{
	cout << Max(1, 2) << endl;
	return 0;
}

内联函数的定义要在调用之前出现,才可以让编译器在编译期间了解上下文,进行代码替换。除此以外,内联函数register变量类似,仅仅是我们提给编译器的一个请求,最终是否真正会实现内联,由编译器根据情况自行选择。


 c++字符串:
  字符数组 
  string 类类型
  字符数组: 使用null字符\0 终止的以为字符数组
  操作字符数组的一些方法:
  strcpy(s1,s2); 复制s2到s1
  strcat(s1,s2); 连接s2到s1的末尾
  strlen(s1); 返回s1的长度
  strcmp(s1,s2); 如果s1和s2相同返回0,大于s2返回正数,小于返回负数
  strchr(s1,ch); 返回一个指针,指向s1中字符ch第一次出现的位置
  strstr(s1,s2);返回一个指针,指向字符串s2第一次出现的位置

 访问权限: public private pritected

 与普通变量一样,对象也是一片连续的内存空间,对象指针存储这个对象的地址
 除了在赋值、访问成员的时候用以外,在传参的时候也建议用指针来传递,因为其传递的为地址,不会进行对象之间的副本赋值,从而减少内存的开销,提高效率

 对象引用就是一个类对象起个别名,本质上也是把这个类对象的地址赋给了这个引用类型,两者指向一块内存空间,不会调用构造函数
 引用类型存的还是地址,传参定义都不会太多内存开销,有指针的优势

 析构函数(destructor):用做对象释放后的清理善后工作
 不能重载,可以是虚函数

// 类
#include<iostream>
using namespace std;
class Student
{
public:
	string name;
	int age;
	int print()
	{
		cout << name << endl;
		return 0;
	}
	int look(); // 类内声明
	// 构造方法
	Student(string name, int age);
	Student();
	//析构函数
	~Student();
};
// 类外定义
int Student::look() {
	return 0;
}
Student::Student(string name, int age) {
	this->name = name;
	this->age = age;
}
Student::~Student() {
	cout << "destructor" << endl;
}
int main()
{
	// 创建对象
	Student liHua;
	liHua.age = 20;
	liHua.name = "lihua";
	liHua.print();
	// 对象指针
	Student* p;
	p = &liHua;
	p->print();
	return 0;
	//对象的引用
	Student& A = liHua;
	A.print();
}

深拷贝与浅拷贝:
 浅拷贝; 把原有对象中的成员依次拷贝给新对象中对应的成员
 但是当成员变量中有指针成员的时候,浅拷贝不会给新的对象的指针成员开辟内存空间,两个指针指向一块堆内存,那么释放这块内存的时候,两个对象调用两次,delete两次就会出现错误
 深拷贝:定义拷贝函数 在实现中开辟内存

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值