virtual

初识继承和多态

▼魔方 西西 提交于 2020-01-10 02:44:14
集成的概念 在C#中,一个类可以继承另一个类,被继承的类通常称为父类或基类。继承其他类的类被称为子类或派生类。派生类的定义可以增加新的数据和功能,派生类的实例也直接可以使用父类的数据或功能,但父类的实例不可以直接使用子类定义的数据或功能 继承是面向对象编程中的一个重要特性。继承关系在类图中表示一个箭头,箭头指向父类如图所示: base关键字和protected修饰符 我们知道this关键字可以表示当前类的实例,通过它可以访问类本身的成员,在C#中还有一个base关键字表示父类,它可以用于访问父类的成员。 如在SE类中可以通过base.Age访问Empployee类的Age属性。 父类中的成员如果用private修饰,它将作为私有成员,其他任何类都无法访问。如果设为public则任何类都可以访问,C#中还提供了另一种修饰符,被这个修饰符修饰的成员允许被其子类访问而不允许被其他非子类访问。 三种修饰符对类成员的限制强度如下: private>protected>public 子类构造函数 隐式调用父类构造函数 在创建子类的实例对象是会先调用父类的构造函数,如果我们不显示的指定调用父类构造函数系统会自动隐式的调用父类的无参构造,如果父类没有无参构造则必须显示的指定调用要调用的父类构造。 显示调用父类的构造函数 上面说到使用base关键字可以表示父类所以只要在子类的构造函数后添加:base

Overriding static members and “static static arrays”

浪子不回头ぞ 提交于 2020-01-07 02:52:26
问题 I've got a tricky issue with "overriding" static arrays. I've got static arrays (for simplicity) that are of fixed length in different derived classes, but still all sizes are known in compile-time. I've also got a virtual function in the base class, but I don't know how to tackle the problem of overriding these arrays and array-sizes in the derived classes so that this virtual function works correctly, i.e. gives the sizes and array contents from the derived classes. Example: class B {

C++ covariance in parameters

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 08:01:12
问题 I wanted to know why C++ does not support co-variance in parameters like in example below or if there is a way to achieve it? class base { public: virtual base* func(base * ptr) { return new base(); } }; class derived : public base { public: virtual derived* func(derived * ptr) override { return new derived(); } //not allowed }; 回答1: The return type is permissible since derived inherits from base , but the function parameter can't work - not all base instances will be a derived also. What's

建造者模式(C++)

孤街浪徒 提交于 2020-01-05 11:09:40
   建造者模式:将复杂的对象的表示和它的实现相分离,使其在同样的构建下可以构建不通的表示。    上面的话可能不是很明白,举个例子就明白了。假设一个公司的软件项目流程是固定的,都需要经过需求理解、需求设计、需求审核、编码、测试这个五个步骤,但是这个公司是一个一级供应商,在拿到项目后,有些模块需要承包给二级供应商,如HMI、Media两个模块需求外包出去,但是根据项目要求,所有模块(包括外包出去的模块)必须要 实施 需求理解、需求设计、需求审核、编码、测试这个基本步骤。   如果这个用面向对象表示出来,那我们可以这样设计,设计一个项目类,包含需求理解、需求设计、需求审核、编码、测试这5个接口;其次设计一个抽象的供应商类,提供5个实现需求理解、需求设计、需求审核、编码、测试的方法,再设计一个HMI类和一个media类,并继承自供应商类,分别 实现 需求理解、需求设计、需求审核、编码、测试这5方法;最后定义一个公司类,公司类里面包含一个供应商类的变量,在公司类里定义一个做项目的方法,在这个方法里分别调用供应商类的需求理解、需求设计、需求审核、编码、测试就实现了。毕竟语言有点苍白,来看看UML类图吧: 再来看看代码就更明白: #include <iostream> #include <string> using namespace std; class Project //项目类 {

protected virtual methods in f#

余生颓废 提交于 2020-01-05 01:11:08
问题 F# does not support the definition of protected methods. Here it is explained why F# replaces virtual methods with abstract methods defined in abstract classes (see here). I was wondering if there is a way to prevent access to abstract methods from outside the derived classes at all. 回答1: Like Patryk Ćwiek, I also don't think it's possible, but here's one alternative: From Design Patterns we know that we should favour Composition over Inheritance . In my experience, everything you can do with

Calling a virtual function on a vector of base classes

依然范特西╮ 提交于 2020-01-04 15:27:32
问题 I created some code to reproduce the problem: #include "stdafx.h" #include <iostream> #include <vector> class A { protected: int m_X; public: A() { std::cout << "in A ctor" << std::endl; m_X = 0; } virtual void printX(){ std::cout << "in A " << m_X << std::endl; } }; class B : public A { public: B() { std::cout << "in B ctor" << std::endl; m_X = 1; } virtual void printX(){ std::cout << "in B " << m_X << std::endl; } }; class As { public: void AddA( const A &a ){ m_As.push_back( a ); } void

Calling a virtual function on a vector of base classes

醉酒当歌 提交于 2020-01-04 15:27:11
问题 I created some code to reproduce the problem: #include "stdafx.h" #include <iostream> #include <vector> class A { protected: int m_X; public: A() { std::cout << "in A ctor" << std::endl; m_X = 0; } virtual void printX(){ std::cout << "in A " << m_X << std::endl; } }; class B : public A { public: B() { std::cout << "in B ctor" << std::endl; m_X = 1; } virtual void printX(){ std::cout << "in B " << m_X << std::endl; } }; class As { public: void AddA( const A &a ){ m_As.push_back( a ); } void

GDB incomplete type when having C++ virtual function

痞子三分冷 提交于 2020-01-04 14:17:43
问题 I have just noticed something weird, when I add the "virtual keyword" in my class (any function except the constructor), I can't display the content of my object in GDB. GDB says "incomplete type" Here is the code : //////////////// reco.h ///////////// #ifndef RECO_H #define RECO_H #include <iostream> #include <string> class reco { public: reco(float weight); ~reco(void); float getWeight(); private: float weight; }; #endif ///////////////// reco.cpp ///////////// #include <iostream> #include

Virtual function breaking private access

自闭症网瘾萝莉.ら 提交于 2020-01-04 06:01:35
问题 I recently came across this article on IBM site. Below is the sample code #include "iostream" class B { public: virtual void f() { std::cout<<"\n In class B"; } }; class D : public B { private: int i; void f() { std::cout<<"\n In class D i = "<<i; } public: D(int i_num):i(i_num) {} }; int main() { D dobj(10); B* bptr = &dobj; D* dptr = &dobj; // valid, virtual B::f() is public, // D::f() is called bptr->f(); // error, D::f() is private //dptr->f(); } We are now able to call private function

sealed,new,virtual,abstract与override

情到浓时终转凉″ 提交于 2020-01-03 23:12:31
1.sealed---“断子绝孙” 密封类不能被继承,不会有子类。密封方法可以重写 基类 中的方法。 2.new---“你是你的,我是我的” new关键字用于显式隐藏从基类继承的成员,也就是说在使用派生类时调用的方法是new关键字新定义出来的方法,而不是基类的方法。在不使用new修饰符的情况下隐藏成员是允许的,但会生成警告。使用new显式隐藏成员会取消此警告,并使用派生类新定义的方法。即:好比是不用祖宗的东西,而是用自己创造的东西。 using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Child c = new Child(); c.SayHello(); //显示我是子类 ((Parent)c).SayHello(); //显示我是父类 } } class Parent { public void SayHello() { Console.Write("我是父类"); } } class Child:Parent { public new void SayHello() { Console.Write("我是子类"); } } } 3.virtual---"为了子孙后代" 用于修改方法或属性的声明,修改后的方法或属性称作虚成员