virtual

虚函数以及纯虚函数

天涯浪子 提交于 2019-12-02 11:27:30
   多态性 是将接口与实现进行分离;用形象的语言来解释就是实现以共同的方法,但因个体差异,而采用不同的策略。   虚函数和纯虚函数都是实现多态的重要方法。本文就这两种方法进行分析以及比较 1、虚函数 在基类中声明为 virtual 并在一个或者多个派生类被重新定义的成员函数 语法规则: virtual 函数返回类型 函数名(参数表) {函数体} 语法分析:虚函数的声明和定义和普通的成员函数一样,只是在返回值之前加入了关键字virtual。      在基类当中定义了虚函数,可以再子类中定义和基类中相同函数名、相同参数、相同返回值和不同实现体的虚函数      定义为虚函数是为了让 基类函数的指针或者引用来指向子类。 #include<iostream> using namespace std; class A { public: void fun() { cout << "A::fun()..." << endl; } }; class B :public A { public: void fun() { cout << "B::fun()...." << endl; } }; int main() { A *a = new A; //A类指针指向A类对象 a->fun(); A *b = new B; //A类指针指向B类 对象 b->fun(); delete a;

多态(C++语言)

倾然丶 夕夏残阳落幕 提交于 2019-12-02 11:24:37
一、多态的定义 派生类对象的地址可以赋值给基类指针。对于通过基类指针调用基类和派生类中都有的同名、同参数表的虚函数的语句,编译时并不确定要执行的是基类还是派生类的虚函数;而当程序运行到该语句时,如果基类指针指向的是一个基类对象,则基类的虚函数被调用,如果基类指针指向的是一个派生类对象,则派生类的虚函数被调用。这种机制就叫作“多态(polymorphism)”。多态可以简单地理解为同一条函数调用语句能调用不同的函数;或者说,对不同对象发送同一消息,使得不同对象有各自不同的行为。 所谓“虚函数”,就是在声明时前面加了 virtual 关键字的成员函数。virtual 关键字只在类定义中的成员函数声明处使用,不能在类外部写成员函数体时使用。静态成员函数不能是虚函数。包含虚函数的类称为“多态类”。 *例如以下程序: #include <iostream> using namespace std; class A { public: virtual void Print() { cout << "A::Print" << endl; } }; class B : public A { public: virtual void Print() { cout << "B::Print" << endl; } }; class D : public A { public: virtual void

c++纯虚函数与抽象基类

一曲冷凌霜 提交于 2019-12-02 11:22:06
首先:强调一个概念 定义一个函数为虚函数,不代表函数为不被实现的函数。 定义他为虚函数是为了允许用基类的指针来调用子类的这个函数。 定义一个函数为纯虚函数,才代表函数没有被实现。 定义纯虚函数是为了实现一个接口,起到一个规范的作用,规范继承这个类的程序员必须实现这个函数。 1、简介 假设我们有下面的类层次: class A { public : virtual void foo ( ) { cout << "A::foo() is called" << endl ; } } ; class B : public A { public : void foo ( ) { cout << "B::foo() is called" << endl ; } } ; int main ( void ) { A * a = new B ( ) ; a - > foo ( ) ; // 在这里,a虽然是指向A的指针,但是被调用的函数(foo)却是B的! return 0 ; } 这个例子是虚函数的一个典型应用,通过这个例子,也许你就对虚函数有了一些概念。它虚就虚在所谓"推迟联编"或者"动态联编"上,一个类函数的调用并不是在编译时刻被确定的,而是在运行时刻被确定的。由于编写代码的时候并不能确定被调用的是基类的函数还是哪个派生类的函数,所以被成为"虚"函数。

What's the difference between virtual function instantiations in C++?

北战南征 提交于 2019-12-02 11:01:11
问题 What's the difference between the following two declarations? virtual void calculateBase() = 0; virtual void calculateBase(); I read the first one (=0) is a "pure abstract function" but what does that make the second one? 回答1: First one is called a pure virtual function. Normally pure virtual functions will not have any implementation and you can not create a instance of a class containing a pure virtual function. Second one is a virtual function (i.e. a 'normal' virtual function). A class

Azure Same Storage on VM

浪子不回头ぞ 提交于 2019-12-02 10:00:32
I want to make 20 Virtual Computers and I want to download same files on them, however it will take too long time to get on each one of them and download the same files. Is there anyway I can have the files already downloaded on the other Virtual machines that I will create so I don't have to re-download on each single one? I'm not sure that I understand what you're trying to do. If you want to create a base image of software that all your VMs will share, you should be able to set up a single virtual machine, then sysprep the image, capture it, and use that to spin up new virtual machines with

Virtual function keyword

时光总嘲笑我的痴心妄想 提交于 2019-12-02 09:33:17
问题 Is there any difference between declaring inherited virtual function in a child class with the "virtual" keyword or not , considering I want to call fun appropriate to my objects' type. Look at the comments. #include <cstdio> struct A{ int a; A():a(5){} virtual int fun(){return a+1;} }; struct B: public A{ virtual int fun(){return a+5;} //I put virtual here // int fun(){return a+5;} // Any difference if I put virtual before or not? }; int main(){ B obj; printf("%d\n", static_cast<A>(obj).fun(

条款07:为多态基类声明virtual析构函数

試著忘記壹切 提交于 2019-12-02 08:06:07
Declare destructors virtual in polymorphic base classes. 有许多种做法可以记录时间,因此,设计一个TimeKeeper base class和一些derived classes作为不同的计时方法,相当合情合理: class TimeKeeper { public: TimeKeeper(); ~TimeKeeper(); }; class AtomicClock:public TimeKeeper {...};//原子钟 class WaterClock:public TimeKeeper {...};//水钟 class WristWatch:public TimeKeeper {...} //腕表 许多客户只想在程序中使用时间,不想操心时间如何计算等细节,这时候我们可以设计factory(工厂)函数,返回指针指向一个计时对象。Factory函数会“返回一个base class指针,指向新生成的derived class对象”:TimeKeeper *getTimeKeeper(); 为遵守factory函数的规范,被getTimeKeeper()返回的对象必须位于heap。因此为了避免泄露内存和其他资源,将factory函数返回的每一个对象适当的delete掉很重要: TimeKeeper* ptk =

vsftpd在Ubuntu 64位下的多用户多目录配置

陌路散爱 提交于 2019-12-02 07:38:58
更多内容请看: http://www.bdtool.net/ 研究了两天vsftpd,从网上找了很多资料,不是出这问题就是那问题,幸好终于解决了,下面贴出我的配置过程。不喜勿喷。。。 一、安装vsftpd sudo apt-get install vsftpd 安装完以后大致的目录介绍 /etc/vsftpd/vsftpd.conf 主配置文件 /usr/sbin/vsftpd Vsftpd的主程序 /etc/rc.d/init.d/vsftpd 启动脚本 /etc/pam.d/vsftpd PAM认证文件(此文件中file=/etc/vsftpd/ftpusers字段,指明阻止访问的用户来自/etc/vsftpd/ftpusers文件中的用户) /etc/vsftpd/ftpusers 禁止使用vsftpd的用户列表文件。记录不允许访问FTP服务器的用户名单,管理员可以把一些对系统安全有威胁的用户账号记录在此文件中,以免用户从FTP登录后获得大于上传下载操作的权利,而对系统造成损坏。(注意:linux-4中此文件在/etc/目录下) /etc/vsftpd/user_list 禁止或允许使用vsftpd的用户列表文件。这个文件中指定的用户缺省情况(即在/etc/vsftpd/vsftpd.conf中设置userlist_deny=YES)下也不能访问FTP服务器

Virtual functions and polymorphism

拈花ヽ惹草 提交于 2019-12-02 07:24:33
Suppose I have this: class A { public: virtual int hello(A a); }; class B : public A { public: int hello(B b){ bla bla }; }; So, A it's an abstract class. 1)In the class B, I'm defining a method that its suppose overrides the A class. But the parameter it's slightly different. I'm not sure about this, is this correct? Maybe because of polymorphism, this is ok but its rather confusing. 2) If I do: A a = new B;, and then a.hello(lol); if "lol" it's not of type B, then it would give compile error?, and if it's of type A from another class C (class C : public A), what would happend? I'm confused

Calling a virtual function on a reference

拥有回忆 提交于 2019-12-02 07:22:25
In the following code, why does the last call of eat() on the reference c return " An animal b is eating. " ? From my understanding, c is a reference to instance b of the derived class Dog and eat() is a virtual function. So it should have returned "A dog b is eating." #include <string> #include <iostream> using namespace std; class Animal { protected: string name; public: Animal( string _name ): name(_name) { } virtual void eat() { cout << "An animal " << name << " is eating." << endl; } }; class Dog : public Animal { public: Dog( string _name ): Animal(_name) { } void eat() { cout << "A dog