Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,582 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,913 people online right now. Registration is fast and FREE... Join Now!




Virtual Function / Abstract Class

 
Reply to this topicStart new topic

Virtual Function / Abstract Class

COMMUNISTCHINA
27 Sep, 2008 - 04:05 PM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 10


My Contributions
For class, we are analyzing 4 different data structures. Each data structures holds randomly generated integers. Later, we compare them with Big-O, and give the advantages and disadvantages of each one.

It makes sense that each data structure contains the same integers, but how do I make sure they all have the same data?

Here's an example:

Abstract class...
CODE
#pragma once
#include <iostream>
#include <cstdlib> // random generation
#include <ctime> // random generation

using namespace std;

class AbstractStruct
{
public:

    virtual int generate_data(int n) = 0;
    virtual void sort_data(void) = 0;




};



This class is called vector_class, and in the vector_class.h file, I have this:
CODE
class vector_class: public AbstractStruct


In vector_class.cpp, I have:
CODE
int vector_class::generate_data(int n)
{
    data_vector;
    data_vector.reserve(n);

    srand((unsigned)time(0));

    for(int i = 0; i < n; i++)
    {
        int data = rand();
        data = data<<15;
        data+=rand();
        data_vector.push_back(data);

        cout << data_vector.at(i) << endl;
    }

    return 0;
}


How can I get the data to be the same?
Somehow I need to put data in AbstractStruct, but I'm not sure how to make sure each data structure gets the same data.

This post has been edited by COMMUNISTCHINA: 27 Sep, 2008 - 08:51 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Virtual Function / Abstract Class
27 Sep, 2008 - 06:16 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well you do realize that you can put static const data in AbstractStruct, make it protected and then any class can inherit from the class and use a function to then access this info right?

abstractstruct.h

cpp

#pragma once
#include <iostream>
#include <cstdlib> // random generation
#include <ctime> // random generation

using namespace std;

class AbstractStruct
{
protected:
// Hardcoded a value that now all inheriting classes will
// have access to. It is const, so it is not changeable.
static const int blah = 5;
public:

virtual int generate_data(int n) = 0;
virtual void sort_data(void) = 0;

};


Then of course you can inherit from it to form vector_class. In vector_class you will have a function like int getblah() which will then simply return the variable "blah".

If you want another structure to have these values, again you inherit from AbstractStruct and you get access to this data.

Hopefully this will serve your purposes. smile.gif
User is offlineProfile CardPM
+Quote Post

COMMUNISTCHINA
RE: Virtual Function / Abstract Class
27 Sep, 2008 - 07:50 PM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 10


My Contributions
QUOTE(Martyr2 @ 27 Sep, 2008 - 09:16 PM) *

Well you do realize that you can put static const data in AbstractStruct, make it protected and then any class can inherit from the class and use a function to then access this info right?

abstractstruct.h

cpp

#pragma once
#include <iostream>
#include <cstdlib> // random generation
#include <ctime> // random generation

using namespace std;

class AbstractStruct
{
protected:
// Hardcoded a value that now all inheriting classes will
// have access to. It is const, so it is not changeable.
static const int blah = 5;
public:

virtual int generate_data(int n) = 0;
virtual void sort_data(void) = 0;

};


Then of course you can inherit from it to form vector_class. In vector_class you will have a function like int getblah() which will then simply return the variable "blah".

If you want another structure to have these values, again you inherit from AbstractStruct and you get access to this data.

Hopefully this will serve your purposes. smile.gif


That's sort of what I want, but I need the integers to be randomly generated. The number of needed integers is unknown until the user passes through how many is desired.
How can I access the same random integers?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:33AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month