程序设计与算法(三)C++面向对象程序设计 第二周 相关笔记
2021/10/7 14:12:37
本文主要是介绍程序设计与算法(三)C++面向对象程序设计 第二周 相关笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、
成员函数可写在类的外面
#include<bits/stdc++.h> using namespace std; class student{ public: int grade; string name; void init_(string _name,int _grade){ name = _name; grade = _grade; } void get_name(){ cout<<name<<endl; } void get_grade(){ cout<<grade<<endl; } void together(); }; void student::together(){ cout<<name<<" "<<grade<<endl; } int main(){ student stu; stu.init_("Bruce",99); stu.get_grade(); stu.together(); return 0; }
2、
类的成员
有pubilc,private,protected 没有次序,默认private
3、
成员函数同样可以重载
4、构造函数
(1)必须public 与类同名 类生成就进行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
(2)重载
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class test{ public: test(int x){ cout<<"first called"<<endl; } test(){ cout<<"second called"<<endl; } }; int main(){ test t1; cout<<endl; test t2[2] = {1,2}; cout<<endl; test t3[2] = {1}; cout<<endl; test * t4= new test[3]; delete []t4; return 0; }View Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class test{ public: test(int x,int y){ cout<<"first called"<<endl; } test(int x){ cout<<"second called"<<endl; } test(){ cout<<"third called"<<endl; } }; int main(){ test t1; cout<<endl; test t2[2] = {1,test(1,2)}; // ! cout<<endl; test t3[2] = {1}; cout<<endl; test * t4= new test[3]; cout<<endl; test * t5[3] = { new test(),new test(1),new test(1,2) }; delete []t4; return 0; }View Code
5、
复制构造函数的三种使用方法
(1)初始化时
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class student{ public: int grade; string name; student (){ ; } student (student &a); void get_name(){ cout<<name<<endl; } void get_grade(){ cout<<grade<<endl; } void together(); }; void student::together(){ cout<<name<<" "<<grade<<endl; } student::student(student &a){ // 构造函数 name = a.name; grade = a.grade; cout<<"Copy constructor run!"<<endl; } int main(){ student stu1; stu1.name = "Bruce"; stu1.grade = 99; student stu2(stu1); return 0; }View Code
(2)作为形参时
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class student{ public: int grade; string name; student (){ ; } student (student &a); void get_name(){ cout<<name<<endl; } void get_grade(){ cout<<grade<<endl; } void together(); }; void student::together(){ cout<<name<<" "<<grade<<endl; } student::student(student &a){ // 构造函数 name = a.name; grade = a.grade; cout<<"!!!"<<endl; } void func(student b){ return ; } int main(){ student stu1; stu1.name = "Bruce"; stu1.grade = 99; student stu2(stu1); func(stu2); return 0; }View Code
(3)作为函数返回值
pass;
赋值时不用到构造函数 如 a = b
调用构造函数耗时间,可以用引用,怕更改原来被引用的还可以const &A
6、
类型转换构造函数
一个参数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class Complex{ double real,imag; public: // 类型转换构造函数 Complex(double i){ real = i; imag = 0; } void output(){ cout<<real<<" + "<<imag<<" i"<<endl; } }; int main(){ Complex c = 1; c.output(); return 0; }View Code
7、
析构函数 == 消亡函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class test{ char *p; public: test(){ p = new char [10]; } ~test(){ cout<<"died!"<<endl; delete [] p; } }; int main(){ test t[2]; cout<<"End Main"<<endl; return 0; } End Main died! died!View Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; class test{ char *p; public: test(){ p = new char [10]; } ~test(){ cout<<"died!"<<endl; delete [] p; } }; int main(){ test * p = new test [2]; cout<<"~~~"<<endl; delete []p; cout<<"~~~"<<endl; return 0; } ~~~ died! died! ~~~View Code
在生命周期结束时,运行析构函数
这篇关于程序设计与算法(三)C++面向对象程序设计 第二周 相关笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-07Dify + TiDB Vector,快速构建你的AI Agent
- 2024-07-06有没有什么开源的py项目可以对图像进行分类-icode9专业技术文章分享
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享