I was poking around the CINT examples and noticed a virtual function example. Since I thought I coined the name I thought I would dig a little deeper. Maybe Charles or AIR can explain what is meant by a virtual function.
/* -*- C++ -*- */
/*************************************************************************
* Copyright(c) 1995~2005 Masaharu Goto (cint@pcroot.cern.ch)
*
* For the licensing terms see the file COPYING
*
************************************************************************/
//
// virtual function test
//
#include <stdio.h>
#include <stdlib.h>
class A {
public:
int a;
A(int i) { a=i; }
virtual void print(void) { printf("a=%d\n",a); }
virtual void Print(void) { printf("A=%d\n",a); }
};
class B {
public:
int b;
B(int i) { b=i; }
void print(void) { printf("b=%d\n",b); }
void Print(void) { printf("B=%d\n",b); }
};
class C: public B, public A {
public:
int c;
C(int i) : A(i+1) , B(i+2) { c=i; }
void print(void) { printf("c=%d\n",c); }
void Print(void) { printf("C=%d\n",c); }
};
class D: public C {
public:
int d;
D(int i) : C(i+10) { d=i; }
void print(void) { printf("d=%d\n",d); }
void Print(void) { printf("D=%d\n",d); }
};
main()
{
A aobj=A(1);
B bobj=B(101);
C cobj=C(201);
D dobj=D(301);
A *pa,*pc,*pd;
B *PB,*pb;
pa = &aobj;
// fprintf(stderr,"Intentional error, pb below\n");
pb = &bobj;
PB = &bobj;
pc = &cobj;
pd = &dobj;
pa->print();
PB->print();
pc->print();
pd->print();
pa->Print();
PB->Print();
pc->Print();
pd->Print();
}
allbasic@c_basic:~/668428/sys/install/cint-5.18.00/demo/simple $ cint virtualfunc1.c
a=1
b=101
c=201
d=301
A=1
B=101
C=201
D=301
allbasic@c_basic:~/668428/sys/install/cint-5.18.00/demo/simple $
Inherit example
/* -*- C++ -*- */
/*************************************************************************
* Copyright(c) 1995~2005 Masaharu Goto (cint@pcroot.cern.ch)
*
* For the licensing terms see the file COPYING
*
************************************************************************/
//
// class inheritance test 1
//
// constructor, destructor
//
#include <iostream.h>
class string {
private:
char *p;
public:
string(char *set) {
cout << "construct string(" << set << ")\n";
p=(char*)malloc(strlen(set)+1);
strcpy(p,set);
}
int len() { return(strlen(p)); }
char *s() { return(p); }
~string() {
cout << "destruct ~string(" << p << ")\n";
free(p);
}
};
ostream& operator <<(ostream& ios,string& x)
{
ios << x.s() ;
return(ios);
}
class gender {
private:
char *male;
string Gender;
public:
gender(char *set) : Gender(set) {
cout << "construct gender(" << set << ")\n";
male=(char*)malloc(strlen(set)+1);
strcpy(male,set);
}
char *S() { return(male); }
~gender() {
cout << "destruct ~gender(" << male<< ")\n";
free(male);
}
};
class person : public gender {
public:
string address;
string name;
int age;
person(void);
person(char *adr,char *nm,int ag,char *setgender);
};
person::person(void)
:address("SAITAMA"),name("MASAHARU"),gender("MALE")
{
age = 33;
cout << "construct person(" << age << ")\n";
}
person::person(char *adr,char *nm,int ag,char *setgender)
: address(adr) , name(nm) , gender(setgender)
{
cout << "construct person(" << ag << ")\n";
age = ag;
}
main()
{
person me=person("Saitama","Masa",32,"male");
cout << " address=" << me.address
<< " name=" << me.name
<< " age=" << me.age
<< " Gender=" << me.S()
<< "\n";
person *pme;
pme = new person;
cout << " address=" << pme->address
<< " name=" << pme->name
<< " age=" << pme->age
<< " Gender=" << pme->S()
<< "\n";
delete pme;
}
allbasic@c_basic:~/668428/sys/install/cint-5.18.00/demo/simple $ cint inherit1.c
construct string(male)
construct gender(male)
construct string(Saitama)
construct string(Masa)
construct person(32)
address=Saitama name=Masa age=32 Gender=male
construct string(MALE)
construct gender(MALE)
construct string(SAITAMA)
construct string(MASAHARU)
construct person(33)
address=SAITAMA name=MASAHARU age=33 Gender=MALE
destruct ~string(MASAHARU)
destruct ~string(SAITAMA)
destruct ~gender(MALE)
destruct ~string(MALE)
destruct ~string(Masa)
destruct ~string(Saitama)
destruct ~gender(male)
destruct ~string(male)
allbasic@c_basic:~/668428/sys/install/cint-5.18.00/demo/simple $
I keep trying but I'm not seeing how C++ and it's obscure syntax is somehow better for me.