目录
第3课_C++高级编程
抽象类概念
虚函数赋值0,这是纯虚函数。一个类里面有纯虚函数的话,这个类就叫做抽象类,这个类就不能够实例化对象了,抽象类是给派生类定义框架的,给使用类的应用程序定义好接口。
15th_abstract
1th
Human.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) = 0; //虚函数赋值0,这是纯虚函数
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
void wearing(void) {cout<<"wear english style"<<endl; }
void driving(void) {cout<<"drive english car"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
void wearing(void) {cout<<"wear chinese style"<<endl; }
void driving(void) {cout<<"drive chinese car"<<endl; }
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
int main(int argc, char **argv)
{
//Human h; 这个编译会出错的,抽象类不能够实例化对象
Englishman e;
Chinese c;
return 0;
}

Human2.cpp


#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
int a;
public:
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() { cout<<"~Human()"<<endl; }
virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};
class Englishman : public Human {
public:
void eating(void) { cout<<"use knife to eat"<<endl; }
void wearing(void) {cout<<"wear english style"<<endl; }
void driving(void) {cout<<"drive english car"<<endl; }
virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};
class Chinese : public Human {
public:
void eating(void) { cout<<"use chopsticks to eat"<<endl; }
void wearing(void) {cout<<"wear chinese style"<<endl; }
//void driving(void) {cout<<"drive chinese car"<<endl; } 如果这里不定义的话,Chinese仍旧是个纯虚函数,main函数里实例化Chinese对象还是会报错
virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};
class Guangximan : public Chinese {
void driving(void) {cout<<"drive guangxi car"<<endl; }
};
int main(int argc, char **argv)
{
//Human h;
Englishman e;
Guangximan g;
return 0;
}

抽象类界面
一个好的程序应该怎么写出来?
2th
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Chinese{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Englishman {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
main.cpp
#include "Englishman.h"
#include "Chinese.h"
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
e.eating();
c.eating();
return 0;
}
Makefile
Human: main.o Chinese.o Englishman.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o Human

3th
3.1
Chinese.cpp
#include "Chinese.h"
void Chinese::setName(char *name)
{
this->name = name;
}
char *Chinese::getName(void)
{
return this->name;
}
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Chinese{
private:
char *name;
public:
void setName(char *name);
char *getName(void);
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::setName(char *name)
{
this->name = name;
}
char *Englishman::getName(void)
{
return this->name;
}
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Englishman {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
main.cpp
#include "Englishman.h"
#include "Chinese.h"
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
e.eating();
c.eating();
return 0;
}
Makefile
Human: main.o Chinese.o Englishman.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o Human

3.2
上面程序,Chinese和English类里面有很多是重复的,每次都要定义很麻烦。我们定义一个共同的基类,Humun。
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
};
#endif
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
main.cpp
#include "Englishman.h"
#include "Chinese.h"
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
e.setName("Bill");
c.setName("zhangsan");
e.eating();
c.eating();
return 0;
}
Makefile
Human: main.o Chinese.o Englishman.o Human.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o Human

4th
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void){cout<<"use hand to eat"<<endl;} //不声明为虚函数的话,会执行基类里的函数
virtual void wearing(void){}
virtual void driving(void){}
};
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
main.cpp
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);//指针数组
return 0;
}
Makefile
Human: main.o Chinese.o Englishman.o Human.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o Human

5th
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0; //纯虚函数
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
};
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
main.cpp
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
return 0;
}
Makefile
Human: main.o Chinese.o Englishman.o Human.o
g++ -o $@ $^
%.o : %.cpp
g++ -c -o $@ $<
clean:
rm -f *.o Human

6th
使用动态库
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
};
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
public:
void eating(void);
void wearing(void);
void driving(void);
~Englishman();
};
#endif
main.cpp
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Englishman e;
Chinese c;
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
return 0;
}
Makefile
Human: main.o libHuman.so
g++ -o $@ $< -L./ -lHuman
%.o : %.cpp
g++ -fPIC -c -o $@ $<
libHuman.so : Englishman.o Chinese.o Human.o
g++ -shared -o $@ $^
clean:
rm -f *.o Human

7th
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
};
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
setName(name);
this->age = age;
memset(this->address, 0, 100);
strcpy(this->address, address);
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
private:
char address[100];
int age;
public:
void eating(void);
void wearing(void);
void driving(void);
Englishman();
Englishman(char *name, int age, char *address);
~Englishman();
};
#endif
main.cpp
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Englishman e("Bill", 10, "sfwqerfsdfas");
Chinese c;
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
return 0;
}
Makefile
Human: main.o libHuman.so
g++ -o $@ $< -L./ -lHuman
%.o : %.cpp
g++ -fPIC -c -o $@ $<
libHuman.so : Englishman.o Chinese.o Human.o
g++ -shared -o $@ $^
clean:
rm -f *.o Human
上面程序,动态库里面的头文件发生变化,那么应用层要重新编译一下,否则会有问题,(实际工作我编译libiotsdk.so也遇到过),有没有办法应用层只包含基类的头文件,派生类随便修改,实现分层,应用层不用每次都重新编译?答案是有的。

8th
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
};
Human& CreateEnglishman(char *name, int age, char *address);
Human& CreateChinese(char *name, int age, char *address);
#endif
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Human& CreateChinese(char *name, int age, char *address)
{
return *(new Chinese());
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
setName(name);
this->age = age;
memset(this->address, 0, 100);
strcpy(this->address, address);
}
Human& CreateEnglishman(char *name, int age, char *address)
{
return *(new Englishman(name, age, address));
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
private:
char address[100];
int age;
public:
void eating(void);
void wearing(void);
void driving(void);
Englishman();
Englishman(char *name, int age, char *address);
~Englishman();
};
#endif
main.cpp
#include "Human.h"
//#include "Englishman.h"
//#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");
Human& c = CreateChinese("zhangsan", 11, "beijing");
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
return 0;
}
Makefile
Human: main.o libHuman.so
g++ -o $@ $< -L./ -lHuman
%.o : %.cpp
g++ -fPIC -c -o $@ $<
libHuman.so : Englishman.o Chinese.o Human.o
g++ -shared -o $@ $^
clean:
rm -f *.o Human
9th
Human.cpp
#include "Human.h"
void Human::setName(char *name)
{
this->name = name;
}
char *Human::getName(void)
{
return this->name;
}
Human.h
#ifndef _HUMAN_H
#define _HUMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Human {
private:
char *name;
public:
void setName(char *name);
char *getName(void);
virtual void eating(void) = 0;
virtual void wearing(void) = 0;
virtual void driving(void) = 0;
virtual ~Human() {cout<<"~Human"<<endl;} //不能设置为纯虚函数,派生类也需要实现一个Human虚函数,这样就奇怪了
};
Human& CreateEnglishman(char *name, int age, char *address);
Human& CreateChinese(char *name, int age, char *address);
#endif
Chinese.cpp
#include "Chinese.h"
void Chinese::eating(void)
{
cout<<"use chopsticks to eat"<<endl;
}
void Chinese::wearing(void)
{
cout<<"wear chinese style"<<endl;
}
void Chinese::driving(void)
{
cout<<"drive chinese car"<<endl;
}
Chinese::~Chinese()
{
cout<<"~Chinese()"<<endl;
}
Human& CreateChinese(char *name, int age, char *address)
{
return *(new Chinese());
}
Chinese.h
#ifndef _CHINESE_H
#define _CHINESE_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Chinese : public Human{
public:
void eating(void);
void wearing(void);
void driving(void);
virtual ~Chinese();
};
#endif
Englishman.cpp
#include "Englishman.h"
void Englishman::eating(void)
{
cout<<"use knife to eat"<<endl;
}
void Englishman::wearing(void)
{
cout<<"wear english style"<<endl;
}
void Englishman::driving(void)
{
cout<<"drive english car"<<endl;
}
Englishman::~Englishman()
{
cout<<"~Englishman()"<<endl;
}
Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
setName(name);
this->age = age;
memset(this->address, 0, 100);
strcpy(this->address, address);
}
Human& CreateEnglishman(char *name, int age, char *address)
{
return *(new Englishman(name, age, address));
}
Englishman.h
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "Human.h"
using namespace std;
class Englishman : public Human {
private:
char address[100];
int age;
public:
void eating(void);
void wearing(void);
void driving(void);
Englishman();
Englishman(char *name, int age, char *address);
virtual ~Englishman();
};
#endif
main.cpp
#include "Human.h"
//#include "Englishman.h"
//#include "Chinese.h"
void test_eating(Human *h)
{
h->eating();
}
int main(int argc, char **argv)
{
Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");
Human& c = CreateChinese("zhangsan", 11, "beijing");
Human* h[2] = {&e, &c};
int i;
for (i = 0; i < 2; i++)
test_eating(h[i]);
delete &e;
delete &c;
return 0;
}
Makefile
Human: main.o libHuman.so
g++ -o $@ $< -L./ -lHuman
%.o : %.cpp
g++ -fPIC -c -o $@ $<
libHuman.so : Englishman.o Chinese.o Human.o
g++ -shared -o $@ $^
clean:
rm -f *.o Human
函数模板引入
16th_template
下面这个比较大小的函数,返回值不一样,需要重载成千上万个函数吗?
不需要,可使用函数模板。
max.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
T& mymax(T& a, T& b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1, ib = 2;
float fa = 1, fb = 2;
double da = 1, db = 2;
mymax(ia, ib);
mymax(fa, fb);
mymax(da, db);
return 0;
}

有一个问题,main函数里如果只定义 int ia = 1, ib = 2; 发现编译的程序大小为9141字节,再加上后面两行的话,编译的程序大小为9231字节,为什么增加了将近一百个字节大小?


max2.cpp

#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
T& mymax(T& a, T& b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
void add(int a, int b)
{
cout<<"add(int a, int b) ="<<(a+b)<<endl;
}
int main(int argc, char **argv)
{
const int ia = 1;
const int ib = 2;
mymax(ia, ib);
int a = 1;
double b = 2.1;
add(a, b);//传递的参数和定义的类型不一样,产生一个隐式类型转换,从double转换成int
mymax(a, b);//模板函数不支持这种隐式类型转换,编译出错
return 0;
}
函数模板只支持两种隐式转换

max3.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
mymax(ia, ib);
return 0;
}

max4.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T* mymax2(const T* a, const T* b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
char a[]="ab";
char b[]="cd";
mymax(a, b); /* T=char[3] */
mymax2(a, b);
char a2[]="abc";
char b2[]="cd";
//mymax(a2, b2); /* mymax(char[4], char[3]), 无法推导出T: mymax(char& [4], char& [3]), 因为两个参数类型不一样 */
mymax2(a2, b2); /* mymax2(char[4], char[3]), 推导: mymax2(const char *, const char *); */
return 0;
}

max5.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T* mymax2(const T* a, const T* b)
{
cout<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
void test_func(T f)
{
cout<<__PRETTY_FUNCTION__<<endl;
}
int f1(int a, int b)
{
return 0;
}
int main(int argc, char **argv)
{
char a[]="ab";
char b[]="cd";
mymax(a, b); /* T=char[3] */
mymax2(a, b);
char a2[]="abc";
char b2[]="cd";
//mymax(a2, b2); /* mymax(char[4], char[3]), 无法推导出T: mymax(char& [4], char& [3]), 因为两个参数类型不一样 */
mymax2(a2, b2); /* mymax2(char[4], char[3]), 推导: mymax2(const char *, const char *); */
test_func(f1);
test_func(&f1);
return 0;
}

无论传入&f1和 f1,效果都是一样的,表明f1可以转化成指针。
函数模板重载


max6.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
const int& mymax(int& a, int& b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
return 0;
}

max7.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
#if 0
const int& mymax(int& a, int& b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
#endif
template<typename T>
const T mymax(T a, T b)
{
cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
return 0;
}
max8.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
const int& mymax(int& a, int& b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
int *p1=&ia;
int *p2=&ib;
cout<<mymax(p1, p2)<<endl;
return 0;
}

max9.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T mymax(T * a, T* b)
{
cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
return (*a < *b)? *b : *a;
}
const int& mymax(int& a, int& b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
int *p1=&ia;
int *p2=&ib;
cout<<mymax(p1, p2)<<endl;
return 0;
}

max10.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
template<typename T>
const T mymax(const T * a, const T* b)
{
cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
return (*a < *b)? *b : *a;
}
const int& mymax(int& a, int& b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
int *p1=&ia;
int *p2=&ib;
cout<<mymax(p1, p2)<<endl;
return 0;
}

max11.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
#if 0
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
#endif
template<typename T>
const T mymax(const T * a, const T* b)
{
cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
return (*a < *b)? *b : *a;
}
const int& mymax(int& a, int& b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
int *p1=&ia;
int *p2=&ib;
cout<<mymax(p1, p2)<<endl;
return 0;
}

max12.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
/*
int& max(int& a, int& b)
{
return (a < b)? b : a;
}
double& max(double& a, double& b)
{
return (a < b)? b : a;
}
float& max(float& a, float& b)
{
return (a < b)? b : a;
}
*/
template<typename T>
const T& mymax(const T& a, const T& b)
{
cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
#if 0
template<typename T>
const T& mymax(T& a, T& b)
{
cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
return (a < b)? b : a;
}
#endif
template<typename T>
const T mymax(T * a, T* b)
{
cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
return (*a < *b)? *b : *a;
}
char* mymax(char* a, char* b)
{
cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
return strcmp(a, b) < 0? b : a;
}
int main(int argc, char **argv)
{
int ia = 1;
int ib = 2;
cout<<mymax(ia, ib)<<endl;
int *p1=&ia;
int *p2=&ib;
cout<<mymax(p1, p2)<<endl;
char *str1="hello";
char *str2="Hellod";
cout<<mymax(str1, str2)<<endl;
return 0;
}

类模板
17th_class_template


template.cpp
模版,这个字母T表示各种数据类型的集合,统一用一个字母T表示。
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
template<typename T>
class AAA {
private:
T t;
public:
void test_func(const T &t);
void print(void);
};
template<typename T> void AAA<T>::test_func(const T &t)
{
this->t = t;
}
template<typename T>
void AAA<T>::print(void)
{
cout<<t<<endl;
}
int main(int argc, char **argv)
{
AAA<int> a; //类模板
a.test_func(1);
a.print();
AAA<double> b;
b.test_func(1.23);
b.print();
return 0;
}

template2.cpp

#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
template<typename T>
class AAA {
private:
T t;
public:
void test_func(const T &t);
void print(void);
};
template<typename T> void AAA<T>::test_func(const T &t)
{
this->t = t;
}
template<typename T>
void AAA<T>::print(void)
{
cout<<t<<endl;
}
template<>
class AAA<int> {
public:
void test_func_int(const int & t)
{
cout<<t<<endl;
}
void print_int(void);
};
void AAA<int>::print_int(void)
{
cout<<"for test"<<endl;
}
int main(int argc, char **argv)
{
AAA<int> a;
a.test_func_int(1);
a.print_int();
AAA<double> b;
b.test_func(1.23);
b.print();
return 0;
}

异常

18th_exception
exception.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
void C()
{
throw 1;
}
void B()
{
C();
}
void A()
{
try {
B();
} catch (int i)
{
cout<<"catch exception "<<i<<endl;
}
}
int main(int argc, char **argv)
{
A();
return 0;
}

exception2.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
void C(int i)
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else
throw c;
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch exception "<<j<<endl;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}



参数传入2,程序崩溃了,A函数里捕捉int变量,并没有捕捉double(c函数抛出的是double异常),所以异常退出,我们需要完善捕捉功能代码。
exception3.cpp
完善捕捉功能代码
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
void C(int i)
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else
throw c;
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch int exception "<<j<<endl;
} catch (double d)
{
cout<<"catch double exception "<<d<<endl;
} catch (...){
cout<<"catch other exception "<<endl;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}

exception4.cpp
抛出一个类异常
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
class MyException {
public:
void what(void) { cout<<"This is MyException"<<endl; }
};
void C(int i)
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else if (i == 3)
throw c;
else if (i == 4)
throw MyException();
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch int exception "<<j<<endl;
} catch (double d)
{
cout<<"catch double exception "<<d<<endl;
} catch (MyException &e)
{
e.what();
} catch (...){
cout<<"catch other exception "<<endl;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}

exception5.cpp
派生类异常
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
class MyException {
public:
void what(void) { cout<<"This is MyException"<<endl; }
};
class MySubException : public MyException{
public:
void what(void) { cout<<"This is MySubException"<<endl; }
};
void C(int i)
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else if (i == 3)
throw c;
else if (i == 4)
throw MyException();
else if (i == 5)
throw MySubException();//
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch int exception "<<j<<endl;
} catch (double d)
{
cout<<"catch double exception "<<d<<endl;
} catch (MySubException &e)//派生类隐式转化为基类对象 这个要放到前面,先捕捉小范围的,再捕捉大范围的
{
e.what();
} catch (MyException &e)
{
e.what();
} catch (...){
cout<<"catch other exception "<<endl;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}

exception6.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
class MyException {
public:
virtual void what(void) { cout<<"This is MyException"<<endl; }// 多态
};
class MySubException : public MyException{
public:
void what(void) { cout<<"This is MySubException"<<endl; }
};
void C(int i)
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else if (i == 3)
throw c;
else if (i == 4)
throw MyException();
else if (i == 5)
throw MySubException();
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch int exception "<<j<<endl;
} catch (double d)
{
cout<<"catch double exception "<<d<<endl;
} catch (MyException &e)
{
e.what();
} catch (...){
cout<<"catch other exception "<<endl;
}
}
int main(int argc, char **argv)
{
int i;
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}

exception7.cpp
C函数声明可能会扔出哪些异常。
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
class MyException {
public:
virtual void what(void) { cout<<"This is MyException"<<endl; }
};
class MySubException : public MyException{
public:
void what(void) { cout<<"This is MySubException"<<endl; }
};
void C(int i) throw(int, double) //运行输入3 4 5 不在声明异常列表里面,程序会崩溃
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else if (i == 3)
throw c;
else if (i == 4)
throw MyException();
else if (i == 5)
throw MySubException();
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch int exception "<<j<<endl;
} catch (double d)
{
cout<<"catch double exception "<<d<<endl;
} catch (MyException &e)
{
e.what();
} catch (...){
cout<<"catch other exception "<<endl;
}
}
void my_unexpected_func()
{
cout<<"my_unexpected_func"<<endl;
}
void my_terminate_func () { cout<<"my_terminate_func"<<endl; }
int main(int argc, char **argv)
{
int i;
set_unexpected (my_unexpected_func); //自己实现意料之外的异常函数 终止函数
set_terminate(my_terminate_func);
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}


exception8.cpp
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
class MyException {
public:
virtual void what(void) { cout<<"This is MyException"<<endl; }
};
class MySubException : public MyException{
public:
void what(void) { cout<<"This is MySubException"<<endl; }
};
void C(int i) throw(int, double)
{
int a = 1;
double b= 1.2;
float c = 1.3;
if (i == 0)
{
cout<<"In C, it is OK"<<endl;
}
else if (i == 1)
throw a;
else if (i == 2)
throw b;
else if (i == 3)
throw c;
else if (i == 4)
throw MyException();
else if (i == 5)
throw MySubException();
}
void B(int i)
{
cout<<"call C ..."<<endl;
C(i);
cout<<"After call C"<<endl;
}
void A(int i)
{
try {
B(i);
} catch (int j)
{
cout<<"catch int exception "<<j<<endl;
} catch (MyException &e)
{
e.what();
}
}
void my_unexpected_func()
{
cout<<"my_unexpected_func"<<endl;
}
void my_terminate_func () { cout<<"my_terminate_func"<<endl; }
int main(int argc, char **argv)
{
int i;
set_unexpected (my_unexpected_func);
set_terminate(my_terminate_func);
if (argc != 2)
{
cout<<"Usage: "<<endl;
cout<<argv[0]<<" <0|1|2|3>"<<endl;
return -1;
}
i = strtoul(argv[1], NULL, 0);
A(i);
return 0;
}


2211

被折叠的 条评论
为什么被折叠?



