C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。
继承的基本语法: 继承的目的就是用于提高代码的复用性,减少重复代码的开发,提高工作效率,先来定义简单的继承关系.
#include <iostream>
using namespace std;
class BasePage
{
private:
int m_x;
protected:
int m_y;
public:
void header() { cout << "header" << endl; }
void footer() { cout << "footer" << endl; }
};
// 新建一个News类,以公有属性继承BasePage类中的内容
class News :public BasePage
{
public:
void connect() { cout << "connect" << endl; }
};
int main(int argc, char *argv[])
{
News news;
news.header();
news.footer();
system("pause");
return 0;
}
继承中的构造/析构函数: 继承后的构造顺序base start -> news start
析构顺序news end -> base end
.
#include <iostream>
using namespace std;
class BasePage
{
public:
BasePage(){ cout << "base page -> start" << endl; }
~BasePage(){ cout << "base page -> end" << endl; }
};
class News :public BasePage
{
public:
News(){ cout << "news page -> start" << endl; }
~News(){ cout << "news page -> end" << endl; }
};
void call()
{ // 此处如果初始化写在main中,将无法看到析构函数的执行
News news;
}
int main(int argc, char *argv[])
{
call();
system("pause");
return 0;
}
继承下的同名函数调用: 在继承多个类的时候,难免会出现同名函数的冲突问题,在调用是可以指明其作用域.
#include <iostream>
using namespace std;
class BasePage
{
public:
void func(int x, int y)
{
cout << "BasePage -> " << x + y << endl;
}
};
class News :public BasePage
{
public:
void func(int x, int y)
{
cout << "News -> " << x + y << endl;
}
};
int main(int argc, char *argv[])
{
News news;
// 如果想调用父类的func()则需要
news.BasePage::func(10,20);
// 调用子类的则是
news.func(100, 200);
system("pause");
return 0;
}
继承下的静态成员属性: 静态成员属性也可以直接通过作用域方式直接调用.
#include <iostream>
using namespace std;
class BasePage
{
public:
static int m_x;
public:
static void func(int x, int y) {cout << "BasePage -> " << x + y << endl;}
};
class News :public BasePage
{
public:
static int m_x;
public:
static void func(int x, int y) {cout << "News -> " << x + y << endl;}
};
int BasePage::m_x = 100;
int News::m_x = 200;
int main(int argc, char *argv[])
{
News news;
cout << News::m_x << endl;
cout << News::BasePage::m_x << endl;
News::func(10,20);
News::BasePage::func(100,200);
system("pause");
return 0;
}
多继承的调用: 定义Super
派生类,分别继承Base1
和Base2
这两个基类.
#include <iostream>
using namespace std;
class Base1
{
public:
int m_x;
public:
Base1(){ m_x = 10; }
void show() { cout << m_x << endl; }
};
class Base2
{
public:
int m_x;
public:
Base2(){ m_x = 20; }
void show() { cout << m_x << endl; }
};
class Super :public Base1, public Base2
{
public:
int m_y;
public:
Super(){ m_y = 30; }
void display(){ cout << m_y << endl; }
};
int main(int argc, char *argv[])
{
Super super;
cout << super.Base1::m_x << endl;
cout << super.Base2::m_x << endl;
cout << super.m_y << endl;
super.Base1::show();
super.display();
system("pause");
return 0;
}
菱形继承(虚继承): 虚继承可能会出现多个基类中存在相同的成员数据,从而造成内存开销过大,虚基类即可解决开销问题.
#include <iostream>
using namespace std;
class Animal
{
public:
int m_Age;
};
// 虚基类 Sheep
class Sheep :virtual public Animal { };
// 虚基类 Tuo
class Tuo :virtual public Animal { };
// 定义派生类
class SheepTuo :public Sheep, public Tuo { };
int main(int argc, char *argv[])
{
SheepTuo ptr;
ptr.Sheep::m_Age = 10;
ptr.Tuo::m_Age = 20;
cout << ptr.Sheep::m_Age << endl;
cout << ptr.Tuo::m_Age << endl;
cout << ptr.m_Age << endl;
system("pause");
return 0;
}
虚函数实现动态多态: 动态多态的作用是,通过传递不同的类指针,实现调用不同的方法,同一个结构返回不同的状态.
#include<iostream>
using namespace std;
class Animal
{
public:
virtual void speak() { cout << "Animal speak" << endl; }
virtual void eat() { cout << "Animal eat" << endl; }
};
class Cat :public Animal
{
public:
void speak() { cout << "Cat speak" << endl; }
virtual void eat() { cout << "cat eat" << endl; }
};
class Dog :public Animal
{
public:
void speak() { cout << "Dog speak" << endl; }
virtual void eat() { cout << "dog eat" << endl; }
};
void doSpeak(Animal & animal)
{
animal.speak();
}
int main(int argc, char *argv[])
{
Cat cat_ptr;
doSpeak(cat_ptr); // 调用同一个函数,返回不同的结果
Dog dog_ptr;
doSpeak(dog_ptr);
system("pause");
return 0;
}
纯虚函数实现多态: 如果父类中存在纯虚函数,子类继承父类后就必须要实现纯虚函数,父类中存在纯虚函数,该类无法实例化对象,只能被继承后实现,这种类也被叫做抽象类.
#include <iostream>
using namespace std;
class AbstractCalculator
{
public:
int x;
int y;
public:
// virtual int getResult() { return 0; }; // 定义虚函数
virtual int getResult() = 0; // 定义纯虚函数
void Set_Val(int x, int y){ this->x = x; this->y = y; };
};
class AddCalculator :public AbstractCalculator
{
public:
virtual int getResult() // 扩展getResult方法
{
return x + y;
}
};
class SubCalculator :public AbstractCalculator
{
public:
virtual int getResult()
{
return x - y;
}
};
int main(int argc, char *argv[])
{
AbstractCalculator *ptr = new AddCalculator; // 创建一个加法
ptr->Set_Val(10, 20);
cout << "x+y = " << ptr->getResult() << endl;
delete ptr;
ptr = new SubCalculator; // 创建一个减法
ptr->Set_Val(10, 20);
cout << "x-y = " << ptr->getResult() << endl;
delete ptr;
system("pause");
return 0;
}
实现虚析构函数: 在基类的析构函数上加上virtual
将其修饰为虚析构函数,其解决的问题是父类指针释放子类对象时,无法释放掉子类对象中的数据,或者说无法调用到子类对象的析构函数,我们可以利用虚析构来解决这个问题.
#include <iostream>
using namespace std;
class Animal
{
public:
// 此处加上virtual声明为虚析构函数
virtual ~Animal() { cout << "Animal back" << endl; }
};
class Cat :public Animal
{
public:
char *m_name;
public:
Cat(const char *name)
{
this->m_name = new char[strlen(name) + 1];
strcpy(this->m_name, name);
}
~Cat()
{
cout << "Cat back" << endl;
if (this->m_name != NULL)
{
delete[] this->m_name;
this->m_name = NULL;
}
}
};
int main(int argc, char *argv[])
{
Animal *ptr = new Cat("Tomcat");
delete ptr;
system("pause");
return 0;
}
实现纯虚析构函数: 如果函数中出现了纯虚析构函数,那么这个类也算抽象类,不可实例化对象,纯虚析构函数必须要存在实现,并且实现代码必须在类外,因为类内被定义为了纯虚析构函数,所以只能在类外部完成了.
#include <iostream>
using namespace std;
class Animal
{
public:
// 此处加上 virtual 声明为纯虚析构函数
virtual ~Animal() = 0 ;
};
Animal::~Animal()
{
// 纯虚析构函数实现
cout << "Animal back" << endl;
}
class Cat :public Animal
{
public:
char *m_name;
public:
Cat(const char *name)
{
this->m_name = new char[strlen(name) + 1];
strcpy(this->m_name, name);
}
~Cat()
{
cout << "Cat back" << endl;
if (this->m_name != NULL)
{
delete[] this->m_name;
this->m_name = NULL;
}
}
};
int main(int argc, char *argv[])
{
Animal *ptr = new Cat("Tomcat");
delete ptr;
system("pause");
return 0;
}
本文作者: 王瑞
本文链接: https://www.lyshark.com/post/5652995a.html
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!