C++/Class/Derived
Содержание
- 1 A base pointer to access derived objects
- 2 call constructor from parent for three level extending
- 3 Demonstrate pointer to derived class.
- 4 how to pass arguments to the base classes of a derived class by modifying the preceding program
- 5 Simple class with its derived class
- 6 Using pointers on derived class objects.
A base pointer to access derived objects
#include <iostream>
using namespace std;
class BaseClass {
int i;
public:
void setInt(int num) {
i = num;
}
int getInt() {
return i;
}
};
class derived: public BaseClass {
int j;
public:
void setJ(int num) {
j = num;
}
int getJ() {
return j;
}
};
int main()
{
BaseClass *bp;
derived d;
bp = &d; // BaseClass pointer points to derived object
// access derived object using BaseClass pointer
bp->setInt(10);
cout << bp->getInt() << " ";
return 0;
}
call constructor from parent for three level extending
#include <iostream>
#include <string.h>
using namespace std;
class Base
{
public:
Base(char *str) { strcpy(message, str); };
void show_base(void) { cout << message << endl; };
protected:
char message[256];
};
class Level1 : public Base
{
public:
Level1(char *str, char *base) : Base(base) {
strcpy(message, str);};
void show_level1(void) { cout << message << endl; } ;
protected:
char message[256];
};
class Lowest : public Level1
{
public:
Lowest(char *str, char *level1, char *base) :
Level1(level1, base) { strcpy(message, str); };
void show_lowest(void)
{
show_base();
show_level1();
cout << message << endl;
};
protected:
char message[256];
};
int main(void)
{
Lowest bottom("L", "m", "e");
bottom.show_lowest();
}
Demonstrate pointer to derived class.
#include <iostream>
using namespace std;
class BaseClass {
int x;
public:
void setx(int i) {
x = i;
}
int getx() {
return x;
}
};
class DerivedClass : public BaseClass {
int y;
public:
void sety(int i) {
y = i;
}
int gety() {
return y;
}
};
int main()
{
BaseClass *p; // pointer to BaseClass type
BaseClass baseObject; // object of BaseClass
DerivedClass derivedObject; // object of DerivedClass
p = &baseObject; // use p to access BaseClass object
p->setx(10); // access BaseClass object
cout << "Base object x: " << p->getx() << "\n";
p = &derivedObject; // point to DerivedClass object
p->setx(99); // access DerivedClass object
derivedObject.sety(88); // can"t use p to set y, so do it directly
cout << "Derived object x: " << p->getx() << "\n";
cout << "Derived object y: " << derivedObject.gety() << "\n";
return 0;
}
how to pass arguments to the base classes of a derived class by modifying the preceding program
#include <iostream>
using namespace std;
class X {
protected:
int a;
public:
X(int i);
};
class Y {
protected:
int b;
public:
Y(int i);
} ;
class Z : public X, public Y {
public:
Z(int x, int y);
int make_ab(void);
} ;
X::X(int i)
{
a = i;
}
Y::Y(int i)
{
b = i;
}
Z::Z(int x, int y) : X(x), Y(y)
{
cout << "initializing\n";
}
int Z::make_ab(void)
{
return a*b;
}
main(void)
{
Z i(10, 20);
cout << i.make_ab();
return 0;
}
Simple class with its derived class
#include <iostream>
#include <cstring>
using namespace std;
class mybase {
char str[80];
public:
mybase(char *s) {
strcpy(str, s);
}
char *get() {
return str;
}
};
class myderived : public mybase {
int len;
public:
myderived(char *s) : mybase(s) {
len = strlen(s);
}
int getlen() {
return len;
}
void show() {
cout << get() << "\n";
}
};
int main()
{
myderived ob("hello");
ob.show();
cout << ob.getlen() << "\n";
return 0;
}
Using pointers on derived class objects.
#include <iostream>
#include <string.h>
using namespace std;
class B_class {
char name[80];
public:
void put_name(char *s) {strcpy(name, s); }
void show_name(void) {cout << name << " ";}
} ;
class D_class : public B_class {
char phone_num[80];
public:
void put_phone(char *num) {
strcpy(phone_num, num);
}
void show_phone() {cout << phone_num << "\n";}
};
main(void)
{
B_class *p;
B_class B_ob;
D_class *dp;
D_class D_ob;
p = &B_ob;
p->put_name("Thomas Edison");
p = &D_ob;
p->put_name("Albert Einstein");
B_ob.show_name();
D_ob.show_name();
cout << "\n";
dp = &D_ob;
dp->put_phone("555 555-1234");
p->show_name();
dp->show_phone();
return 0;
}