to define a derived class or modify the original class

I want to modify a class in a software.
the class is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Chunk
{
	ChunkID c_id;			///< The ID number (sequence number) of the chunk
	Time c_initiation_time;	///< The time at which the chunk becomes available at the source
public:
	/// The constructor for a chunk
	Chunk(size_t id, Time time) : c_id(id), c_initiation_time(time) {}

	/// Get the chunk ID
	inline ChunkID id() { return c_id; }

	/// Get the chunk initiation time
	inline Time initiation_time() { return c_initiation_time; }

};


I want to add two member variables
and some related member functions
and I need modify some other classes or statement which use the class
so, which choice is better for such kind of modification?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <algorithm>

using namespace std;

class Chunk
{
public:
    virtual void show(){cout << "Old child\n";}
    virtual ~Chunk(){}
};

class NewChunk: public Chunk
{
public:
    virtual void show(){cout << "New child\n";}
};

int main()
{
    auto_ptr <Chunk> ptr(new NewChunk);
    ptr->show();
    return 0;
}


Can you give me result of my program?
I think it's "New child"
So you must use virtual functions and don't remember to use virtual constructor
And another example. Please see in function print(). It wasn't modified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <algorithm>

using namespace std;

class Chunk
{
public:
    virtual void show(){cout << "Old child\n";}
    virtual ~Chunk(){}
};

class NewChunk: public Chunk
{
public:
    virtual void show(){cout << "New child\n";}
};

void print(Chunk* chunk)
{
    chunk->show();
}

int main()
{
    Chunk* oldPtr = new Chunk;
    Chunk* ptr = new NewChunk;
    print(oldPtr);
    print(ptr);
    delete ptr;
    delete(oldPtr);
    return 0;
}
Topic archived. No new replies allowed.