20170803

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 C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for det

此错误的原因是:微软公司自己提供的安全函数scanf_s(),ANSI C中没有scanf_s(),只有scanf()

解决方法总结:1、不用scanf()而改用scanf_s()

                         2、在程序前面加#pragma warning(disable:4996)

                         3、无需加代码,只需在新建项目时取消勾选“SDL检查”即可。

                         

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值