C + + classes and C # class (2)
4, C + + and C # in the virtual function
C + + and C # both support virtual function. In the following C + + example, there are two categories, known as Base (base class) and the Derived (inherited class):
# Include
using namespace std;
class Base
(
public:
void doWork ()
(
cout < <"Base class working";
)
protected:
virtual void doWork1 () = 0;
);
class Derived: public Base
(
public:
void doWork2 ()
(
cout < <"Derived class working";
)
void doWork1 ()
(
cout < <"Dervied pure virtual function working";
)
);
void main ()
(
Derived d;
d.doWork1 ();
)
Base class there are two functions, one is doWork, the other is pure virtual function doWork1. doWork1 can only be inherited base class type to use. Inheriting class (public land inherited from base class) there is a new function doWork2, and inheritance in the base class pure virtual function of the transcendental function doWork1.
In C #, to achieve the same function to be easier. See below C # code:
using System;
abstract class Base
(Public void doWork ()
(Console.WriteLine (”Base class working”);
)
public abstract void doWork1 ();
)
class Derived: Base
(Public void doWork2 ()
(Console.WriteLine (”Derived class working”);
)
public override void doWork1 ()
(Console.WriteLine (”Dervied pure virtual function working”);
)
);
class EntryPoint
(
public static void Main ()
(
Derived d = new Derived ();
d.doWork1 ();
)
)
C # will be defined as abstract base class will doWork1 defined as abstract methods. So get with the C + + pure virtual function the same function. Base class containing only as a base class or derived class function doWork1 beyond use.
Inherited class abstract function doWork1 beyond when adding in function beyond the prefix (override). C # compiler, class inheritance is found in the override keyword, the base class of the same name on the check function. If not directly explicitly call the base class function, the compiler always use the inherited class method.
In order to inherit class members and the direct operation of the base class method, C # base class named as an alias base. Using this alias, inherited class can directly reference the base class members and methods. Examples are as follows:
using System;
class first (
public void writeIt ()
(Console.WriteLine (”Writing from base class”);
)
)
class second: first
(
public second ()
(
base.writeIt ();
)
)
class EntryPoint
(
public static void Main ()
(Second s = new second ();
)
)
In the example above, there are two classes. One is the base class (first), another inherited class (second). When you create second instance of the class, its constructor automatically call the base class writeIt method, using the console Console.WriteLine Print Screen command. This leads to C + + and C # in polymorphism.
5, C + + and C # in polymorphism to polymorphism of entities with a variety of forms. In C + + and C # are very similar in the treatment of polymorphism. Look at a simple example:
C + + version:
# Include
# Include
using namespace std;
class Person
(
public:
Person ()
(
classType = “Person”;
)
friend void ShowType (Person & p);
private:
string classType;
);
class Manager: public Person
(Public:
Manager ()
(ClassType = “Manager”;
)
friend void ShowType (Person & p);
private:
string classType;
);
void ShowType (Person & p)
(Cout <
) Void main ()
(Person p;
Manager m;
ShowType (p);
ShowType (m);)
C # version:
using System;
class Person (
public Person ()
(ClassType = “Person”;
)
public string classType;
)
class Manager: Person
(Public Manager ()
(
classType = “Manager”;
)
public new string classType;
)
class EntryPoint
(Public static void ShowType (ref Person p)
(Console.WriteLine (p.classType);
)
public static void Main ()
(
Person p = new Person ();
Person m = new Manager ();
ShowType (ref p);
ShowType (ref m);
)
)
In the example above, there is a base class Person, one inherited from the base class of Manager class. In the EntryPoint class, the created a static function ShowType, it is expressed as:
public static void ShowType (ref Person p)
Note that parameters in the ref keyword. ref to tell C # compiler to pass a parameter to the ShowType function reference (reference). In C #, if not ref keyword, the function of the parameter default value (value) transfer, will copy the value of a parameter passed to the function to.
In C + +, the parameters passed by reference is expressed as: void ShowType (Person & p)
C + + with “&” symbol that argument makes reference to novice programmers confused, especially those who turn from VB.
In C # the main function (entry point), the creation of two new Person object, p and m:
Person p = new Person ();
Person m = new Manager ();
It is worth mentioning that the new keyword in C # and C + +, use is not the same. In C # in, new only to create an object instance. This object is still created in the management of heap, but does not return the memory address pointing to an object pointer. In the above C # example, to create a 2 Person class object. The second object, m, is Manager class object. It uses the Manager instead of the constructor Person constructor.
The Person class object and the Manager class object reference to the ShowType function, remember, Manager is the successor Person class, but C # polymorphism be expressed as a Person class:
ShowType (ref p);
ShowType (ref m);
ShowType function as used, it deals only with Person objects. C # to tell it that m is a Person object class inheritance, it will be handled m by Person class. So, with p and m as parameters obtained by calling ShowType function output is:
Person
Person
[Translator's Note: This polymorphism explained a bit ridiculous. C # code this is the true meaning of interpretation of the role of the static function, but not a polymorphism. Above a C + + code, you can use as the interpretation of polymorphism. ]
VI Conclusion
I know C # before, I used VB for 4 years and 2 years of C + +. I can responsibly say, C # is my language used in the most dynamic and flexible, and enjoyable language, and it is 100% object-oriented. If you are a C + + programmer, and now want to turn e-commerce programming, or simply just want to change a more modern language, it is C # a. So for three reasons:
If you will use C #, you can create any application: Windows applications, console applications, Web applications and Web services.
All. NET platform using the language be compiled into Intermediate Language (IL), and can be optimized according to system environment.
Very, very easy to convert C + + C #.
Recommended links:
Articles ABOUT Timers And Time Synch
“Anti-terrorist forces” make examples of game scene
Pocket Electronic Translator As Our Assistant
National Listing parallel transmission Unicom iPhone flooding into their weakness
Longhorn Beta1 only COMPATIBLE with two kinds of graphics chips
Rising Baojia An Shovel competitors
Unicom How To Benefit From The Introduction Of Geometric IPhone
Numbers will be allocated to the concept of the sword the veil of players



