2016/10/31

来源:互联网 发布:isp图像处理编程 编辑:程序博客网 时间:2024/05/01 12:28
1631-5 黄加勉 <2016.10.31> 【连续第30天总结】


A.今日任务
1.纯虚函数(100%)
2.抽象类(100%)
3.接口类(50%)


B.具体内容 
1.只有声明没有函数体的虚函数是纯虚函数,格式为virtual 返回值 函数名(参数)= 0;
2.包含纯虚函数的类称为抽象类
3.抽象类不能用来实例化对象,这种属性会被继承,直到纯虚函数被定义
4.只有纯虚函数的类称为接口类,接口类更像是一种协议,一种属性,继承接口类的派生类包含了接口类定义的属性
5.两个程序,第一个尝试了纯虚函数和抽象类,还有一个是从字符串中删去某元素的算法


附代码:


/*虚函数和抽象类实验*/
#include <iostream>
#include <string>
using namespace std;


class caneat
{
public:
virtual void eat() = 0;
};


class canwork
{
public:
virtual void work() = 0;
};


class Person :public caneat, public canwork
{
public:
Person() {}
virtual ~Person() {};
};


class Worker :public Person
{
public:
Worker() {}
virtual ~Worker() {}
virtual void eat()
{
cout << "eat" << endl;
}
virtual void work()
{
cout << "work" << endl;
}
};


int main()
{
Worker *wk = new Worker();
wk->eat();
wk->work();
delete wk;
wk = NULL;
system("pause");
return 0;
}


/*字符串中删去某元素*/
#include <iostream>
#include <string>
using namespace std;


string sub(string str1, string str2);


int main()
{
string str1, str2;
cin >> str1;
cin >> str2;
cout << sub(str1, str2) << endl;
system("pause");
return 0;
}


string sub(string str1, string str2)
{
int i, j;
bool val = 1, flag = 0;
while (val)
{
val = 0;
int x = str1.size();
int y = str2.size();
for (i = 0; i < x - y + 2; i++)
{
if (str1[i] == str2[0])
{
int temp = i + 1;
for (j = 1; j < y; j++)
{
if (str1[temp] != str2[j]) break;
temp++;
}
if (j == y)
{
for (j = i; j < x; j++)
{
if (j < x - y)
{
str1[j] = str1[j + y];
}
else if (j >= x - y)
{
str1[j] = ' ';
}
}
val = 1;
flag++;
}
}
}
}
if (flag)
{
return str1;
}
else
{
return "this element is not included";
}
}


C.明日任务
1.接口类
2.RTTI
3.异常处理


0 0