| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | #defines |
Hi steven p
sp> hehehe I know... I used to be rather unimaginative in my variable names
sp> back when I used AmigaBASIC... It built a table of all the variables
sp>
hey, we all gotta learn somewhere... I learned C from a pirated
program from one shop, and a pirated manual from another.
RM> /* example class implementation */
RM> Base B1(20);
RM> Derived D1(25,4);
RM> /* end */
sp> Now, one thing I don't think you addressed (or maybe it's just that I'm
sp> reading this in the early hours of the morning again):
sp> int StuffMe(Base *Goo)
sp> {
sp> return Goo->ask3();
sp> }
sp> can we pass D1 to StuffMe? I tried, but failed. Maybe I just did
sp> something stoopid...
Is the Goo that you *pass* to StuffMe a class or a pointer to a
class? That's the most likely problem. Pointers can be a real pain
sometimes, and sheer bliss at other times.
And was it a compiler error, or did it not work right when you ran
it?
You sod. You've made me go and actually *compile* the drivel I
wrote . The compiler told me that the class declarations in the
originals lacked trailing ;s... The following compiles under BC++4
with one warning, and runs ok. (Btw, it doesn't need splitting into
separate header and source files, it works fine as one file.)
/* start */
/* header file */
class Base
{
public:
Base(int howbig); // constructor
int ask1(void); // functions
int ask2(void);
virtual int ask3(void); // virtual function
int dothings(void);
int a,b; // public variables - anyone can see/alter them
protected:
int c,d,e; // protected variables, derived classes can see them
private:
int f,x; // variables local to Base, derived classes can't see them
};
class Derived : public Base
{
public:
Derived( int howbig, int whatmode); // different constructor from above
int ask2(void); // completely overrides ask2() in Base
int ask3(void); // redefinition of virtual function - see below
protected:
int g; // new variable in Derived, doesn't exist in Base
int x; /* new here, has no relation to x in Base, because that x
is private. Confusing technique, avoid if possible. */
};
/* end header file */
/* main file */
#include
/* class definitions */
Base::Base(int howbig) // constructor
{
a = howbig; // remember the parameter passed to the constructor
b = 0; // initialize a class variable
e = 255;
}
int Base::ask1(void) // typical member function
{ return d; }
int Base::ask2(void) // another typical member function
{ return e; }
int Base::ask3(void) // same as above
{ return e; }
int Base::dothings(void)
{
int i = ask3(); // calls a virtual function - see below
int j = ask2();
return i;
}
Derived::Derived (int howbig, int whatmode) :
Base(howbig) // constructor showing inheritance from Base
{ g = whatmode; } // remember the extra parameter passed to this constructor
int Derived::ask2(void) // overrides ask2() in Base - see below
{ return g; }
int Derived::ask3(void) // overrides ask3() in Base - see below
{ return g; }
int stuffme( Base *b) // takes a pointer to a Base class
{
int i = b->ask3(); // let's use the virtual function...
return(i);
}
int main(void)
{
int i,j,k,l;
Base B1(20);
Derived D1(25,4);
Base *B2 = new Base(40);
Derived *D2 = new Derived(1,1);
i = stuffme(&B1); // address of class required
j = stuffme(&D1);
k = stuffme(B2); // B2 is already an address
l = stuffme(D2);
printf( "i,j,k,l are %d, %d, %d, %d\n", i,j,k,l);
return 0;
}
/* end */
Cheers
--- PPoint 1.88
* Origin: Silicon Heaven (3:711/934.16)SEEN-BY: 633/267 270 @PATH: 711/934 808 50/99 635/728 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.