上一篇文章我把自己写成了一个类,但是谈到继承这个问题以我这个类不好解释,于是我写了一个Book类和他的两个派生类MiniBook和BigBook,用这个实例向你展示继承以及多态的问题。
1.先聊继承,简单看一下运行结果及工程框架
完整代码:
book.h
#ifndef __BOOK_H__
#define __BOOK_H__
#include
namespace paper {
class Book
{
public:
Book()
{
number = 100;
material = "paper";
classification = "Development";
}
~Book()
{
}
void set_number(int n);//设置页数
void set_material(std::string m);//设置材质
void set_classification(std::string c);//设置类别
int get_number();//获取页数
std::string get_material();//获取材质
std::string get_classification();//获取类别
void self_introduction();//这个类可以自我介绍
private:
int number;//页数
std::string material;//材质
std::string classification;//类别
};
}
#endif
book.cpp
#include
#include
namespace paper{
void Book::set_number(int n)
{
number = n;
}
void Book::set_material(std::string m)
{
material = m;
}
void Book::set_classification(std::string c)
{
classification = c;
}
int Book::get_number()
{
return number;
}
std::string Book::get_material()
{
return material;
}
std::string Book::get_classification()
{
return classification;
}
void Book::self_introduction()
{
std::cout << "I am a book...(*_*)" << std::endl;
std::cout << "My number:" << get_number() << std::endl;
char my_material_char[100];
int i;
for (i = 0; i < get_material().length(); i++)
{
my_material_char[i] = get_material()[i];
}
my_material_char[i] = '\0';
std::cout << "My material:" << my_material_char << std::endl;
char my_classification_char[100];
int j;
for (j = 0; j < get_classification().length(); j++)
{
my_classification_char[j] = get_classification()[j];
}
my_classification_char[j] = '\0';
std::cout << "My classification:" << my_classification_char << std::endl;
}
}
minibook.h
//派生类minibook
#ifndef __MINIBOOK_H__
#define __MINIBOOK_H__
#include
#include
namespace paper {
class MiniBook :public Book {
public:
MiniBook()
{
book_size = 1;
}
~MiniBook()
{
}
void set_book_size(int s);
int get_book_size();
private:
int book_size;
};
}
#endif
minibook.cpp
#include
#include
namespace paper {
void MiniBook::set_book_size(int s)
{
book_size = s;
}
int MiniBook::get_book_size()
{
return book_size;
}
}
main.cpp
#include
#include
int main(int argv, char* argc[])
{
//栈创建
paper::MiniBook minibook;
minibook.set_number(10);
minibook.set_material("iron");//设置minibook的材质为钢铁
minibook.set_classification("scientific");//设置minibook的分类为科学的
minibook.set_book_size(1);
std::cout << "minibook book_size:" << minibook.get_book_size() << std::endl;
minibook.self_introduction();//书的自我介绍继承于父类
}
2.多态是什么?用自己的话表述就是父类中的同一个函数在不同子类中实现的功能不同。函数名前面加个virtual,虚函数。
完整代码:
book.h
#ifndef __BOOK_H__
#define __BOOK_H__
#include
namespace paper {
class Book
{
public:
Book()
{
number = 100;
material = "paper";
classification = "Development";
}
~Book()
{
}
void set_number(int n);//设置页数
void set_material(std::string m);//设置材质
void set_classification(std::string c);//设置类别
int get_number();//获取页数
std::string get_material();//获取材质
std::string get_classification();//获取类别
virtual void self_introduction();//这个类可以自我介绍,实现多态virtual,虚函数
private:
int number;//页数
std::string material;//材质
std::string classification;//类别
};
}
#endif
book.cpp
#include
#include
namespace paper{
void Book::set_number(int n)
{
number = n;
}
void Book::set_material(std::string m)
{
material = m;
}
void Book::set_classification(std::string c)
{
classification = c;
}
int Book::get_number()
{
return number;
}
std::string Book::get_material()
{
return material;
}
std::string Book::get_classification()
{
return classification;
}
void Book::self_introduction()
{
std::cout << "I am a book...(*_*)" << std::endl;
std::cout << "My number:" << get_number() << std::endl;
char my_material_char[100];
int i;
for (i = 0; i < get_material().length(); i++)
{
my_material_char[i] = get_material()[i];
}
my_material_char[i] = '\0';
std::cout << "My material:" << my_material_char << std::endl;
char my_classification_char[100];
int j;
for (j = 0; j < get_classification().length(); j++)
{
my_classification_char[j] = get_classification()[j];
}
my_classification_char[j] = '\0';
std::cout << "My classification:" << my_classification_char << std::endl;
}
}
bigbook.h
//派生类bigbook
#ifndef __BIGBOOK_H__
#define __BIGBOOK_H__
#include
#include
namespace paper {
class BigBook :public Book {
public:
BigBook()
{
book_size = 1;
}
~BigBook()
{
}
void set_book_size(int s);
int get_book_size();
virtual void self_introduction()
{
std::cout << "I am bigbook(o_o)" << std::endl;
}
private:
int book_size;
};
}
#endif
bigbook.cpp
#include
#include
namespace paper {
void BigBook::set_book_size(int s)
{
book_size = s;
}
int BigBook::get_book_size()
{
return book_size;
}
}
minibook.h
//派生类minibook
#ifndef __MINIBOOK_H__
#define __MINIBOOK_H__
#include
#include
namespace paper {
class MiniBook :public Book {
public:
MiniBook()
{
book_size = 1;
}
~MiniBook()
{
}
void set_book_size(int s);
int get_book_size();
virtual void self_introduction()
{
std::cout << "I am minibook(0_0)" << std::endl;
}
private:
int book_size;
};
}
#endif
minibook.cpp
#include
#include
namespace paper {
void MiniBook::set_book_size(int s)
{
book_size = s;
}
int MiniBook::get_book_size()
{
return book_size;
}
}
main.cpp
#include
#include
#include
#include
int main(int argv, char* argc[])
{
栈创建
//paper::MiniBook minibook;
//minibook.set_number(10);
//minibook.set_material("iron");//设置minibook的材质为钢铁
//minibook.set_classification("scientific");//设置minibook的分类为科学的
//minibook.set_book_size(1);
//std::cout << "minibook book_size:" << minibook.get_book_size() << std::endl;
//minibook.self_introduction();//书的自我介绍继承于父类
//堆创建(new来分配,delete来释放,我这里就没写释放)
paper::Book* book = new paper::MiniBook();
if (book)
{
book->self_introduction();
}
book = new paper::BigBook();
if (book)
{
book->self_introduction();
}
book = new paper::Book();
if (book)
{
book->self_introduction();
}
}
3.关于指针、内存、拷贝
/*内存和指针
(1)内存地址空间
0x00000000 (操作系统使用的内存空间) 0x80000000 (我们可以访问的空间) 0xFFFFFFFF
32=4*8 4个字节
(2)指针/地址 例如:0x00000000 0x80000000 0xFFFFFFFF
(3)堆空间与栈空间
代码->堆空间->函数参数(top)<-栈空间(本地变量,返回地址,函数参数)
堆空间是由低地址到高地址 new来分配 delete/delete[]来释放
栈空间是由高地址到低地址
(4)内存申请与释放
new来分配 delete/delete[]来释放
(5)深拷贝与浅拷贝
浅拷贝:有一个变量中有指针,只拷贝变量内容,不拷贝变量指针所指内容
深拷贝:都拷贝,并且申请了一个新的指针
浅拷贝a 0x1 2 ->
0x80000012 123
a 0x1 2 ->
深拷贝a 0x24-> 0x80000024 123
*/