|
Many complicated features in C++ are intentionally left out
to retain the simplicity of Ch.
The following C++ features are available in Ch:
-
Class.
-
Member function.
-
Mixed code and declaration.
-
The this-> pointer.
-
Reference type and pass-by-reference.
-
Function-style type conversion.
-
The private/public data and functions in class. Ch is compatible with C++ that
by default, members of a class definition are assumed to
be private until a `public' declaration is given.
-
Static member of class/struct/union.
-
The new and delete operators.
-
The constructor and destructor.
-
Polymorphic functions.
-
The scope resolution operator :: .
-
The I/O cout, cerr, cin with endl.
Ch supports classes in C++ with the following additional capabilities:
Classes inside member functions.
Nested functions with classes.
Pass member function to argument of pointer-to-function type of functions.
#include <stdio.h>
#include <new.h>
#include <iostream.h>
class tag{
private:
int m_i;
public:
tag(int i);
~tag();
int func();
};
tag::tag(int i) {
cout << "i passed to tag::tag() is " << i << endl;
m_i = i;
}
tag::~tag() {
cout << "m_i in tag::~tag() is " << m_i << endl;
}
int tag::func() {
printf("m_i in tag::func() is %d\n", m_i);
return 0;
}
int main() {
class tag s = tag(10);
class tag *sp = new tag(20);
s.func();
sp->func();
delete sp;
return 0;
}
The output is:
i passed to tag::tag() is 10
i passed to tag::tag() is 20
m_i in tag::func() is 10
m_i in tag::func() is 20
m_i in tag::~tag() is 20
m_i in tag::~tag() is 10
|