20170803
来源:互联网 发布:java开发安卓应用 编辑:程序博客网 时间:2024/06/05 15:39
1.String函数:
#ifndef CSTRING_H_#define CSTRING_H_class CString{public: CString(); CString(const char* str); CString(const CString &other); ~CString(); CString& operator =(const CString &str);public: void ShowString();private: char * __Data;};#endif
#include "CString.h"#include "iostream"using namespace std;CString::CString(){ __Data = new char[1]; *__Data = '\0';}CString::CString(const char* str){ int Length = strlen(str); __Data = new char[Length + 1]; for (int i = 0; i < Length; i++) { __Data[i] = str[i]; } __Data[Length] = '\0';}CString::CString(const CString &other){ int Length = strlen(other.__Data); __Data = new char[Length + 1]; for (int i = 0; i < Length; i++) { __Data[i] = other.__Data[i]; } __Data[Length] = '\0';}CString::~CString(){ delete[] __Data;}CString& CString::operator=(const CString &str){ if (this == &str) return *this; delete[] __Data; int Length = strlen(str.__Data); __Data = new char[Length + 1]; for (int i = 0; i < Length; i++) { __Data[i] = str.__Data[i]; } __Data[Length] = '\0'; return *this;}void CString::ShowString(){ cout << __Data << endl;}
#include "CString.h"#include <string> #include "iostream"using namespace std;int main(){ //无参数 CString s; //有参数 CString s1 ("1,2,3,4"); s1.ShowString(); //拷贝构造函数 CString s2(s1); s2.ShowString(); //重载= CString s3 = "3,4,5,6"; CString s4 = s3 = s1; s3.ShowString(); s4.ShowString(); getchar(); return 0;}
2.报错:没有找到接受"std::string"类型的右操作数的运算符
解决方法:#include <string>
问题原因:将C中的string,h头文件在C++中使用,导致运算符对>>对string不能辨别。所以应在C++中避免包括C标准库中的.h文件。
3.scanf与scanf_s的区别:
error
此错误的原因是:微软公司自己提供的安全函数scanf_s(),ANSI C中没有scanf_s(),只有scanf()
解决方法总结:1、不用scanf()而改用scanf_s()
scnaf()在读取时不检查边界,可能会存在内存泄漏的问题,而scanf_s()在读取时检查边界是一种更加安全的函数
scanf一般可能遇到的问题:http://bbs.csdn.net/topics/310149436
4.Strcpy函数
#include <assert.h>char * Strcpy(char* dest, const char* src){ assert(dest != NULL&&src != NULL); char* res = dest; //char aa = *dest++; //指针后移1位 输出为?23abc 第一个位置存放的是随机值,共占用13个位置 while ((*dest++ = *src++) != '\0'); return res;}int main(){ char a[] = "1,2,3,a,b,c"; char d[14]; Strcpy(d, a); cout <<d; system("pause"); return 0;}
5.虚拟继承
1.为什么要有虚拟继承?:为了避免多重继承过程中对于基类成员的重复继承。
2.对含有虚函数的类求类大小的一般规律?
答:1.如果类中有虚函数,则需要一个4字节的指针将虚函数指向自己的类。
2.如果派生类虚继承了基类,则需要一个4字节的虚类指针将子类的虚函数加进来。
3.如果派生类直接继承基类,只看自己的空间大小即可。
推荐博客:http://www.cnblogs.com/BeyondAnyTime/archive/2012/06/05/2537451.html
6.重载,覆盖和隐藏的区别
答:重载:在同一个类中,函数名相同,参数不同,virtual有无都可以。
覆盖 : 在基类和派生类中,函数名相同,参数相同,必须有virtual。
隐藏: 在基类和派生类中,函数名相同,参数相同时:必须没有virtual
参数不同时:有无virtual都可以
阅读全文
0 0
- 20170803
- 20170803
- 20170803题目
- 20170803~20170804
- SQL语法学习20170803
- 20170803工作日记
- 酒客项目日志20170803
- 初步了解CC概念---20170803
- 一起从零学Kotlin-20170803
- Linux学习笔记——20170803
- 20170803工作记账流水(2017-08-03 10:00)
- 新手入门-20170803-Docker 下 LNMP 的搭建(分离各个组件)
- Mark-20170803-git(每次都要重新找链接太麻烦了,不如整理一下步骤)
- 廖雪峰《python3 基础教程》读书笔记——第八章 面向对象编程
- Git客户端工具TortoiseGit安装
- 查找元素——找x
- Shell常用命令
- 解决Hash冲突的几种方法
- 20170803
- JavaScript的学习(五)--函数事件
- js map
- eclipse最常用快捷键
- Android ramdisk中添加自己编写的程序或文件
- RxAndroid菜鸟必入
- Error java Compilation failed internal java compiler error
- 深度学习:正则化方法
- ant的好处