abstract_class2.vala added

This commit is contained in:
Kolan Sh 2014-07-04 18:52:30 +04:00
parent 83d28daa30
commit c5cef9fe2b
2 changed files with 73 additions and 0 deletions

37
cpp/figure/figure.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
using namespace std;
class Figure {
public:
void draw () {
cout << "Figure:draw () called" << endl;
}
};
class Circle : public Figure {
public:
void draw () {
cout << "Circle:draw () called" << endl;
}
};
class ColorCircle : public Circle {
public:
void draw () {
cout << "ColorCircle:draw () called" << endl;
}
};
int main () {
Figure *fig = new ColorCircle ();
fig->draw ();
Circle fig2 = *(Circle*)fig;
fig2.draw ();
return 0;
}

View File

@ -0,0 +1,36 @@
public interface Drawable : Object {
public abstract void draw ();
}
public abstract class Figure : Object, Drawable {
public virtual void draw () {
stdout.printf ("Figure:draw() called\n");
}
}
public class Circle : Figure {
public override void draw () {
stdout.printf ("Circle:draw() called\n");
}
}
public class ColorCircle : Circle {
public override void draw () {
stdout.printf ("ColorCircle:draw() called\n");
}
}
void main () {
var cc = new ColorCircle ();
cc.draw ();
Circle c = cc;
c.draw ();
Figure f = c;
f.draw ();
Drawable d = f;
d.draw ();
}