![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
Pointer to objects inside other objects..
I have this code.
#include <iostream>
class one{
int a;
public:
void set(); // give an integer number to a
void show(); // show the integer
};
class two{
float a;
public:
void set();//give a floating point number to b
void show(); // show the float
};
union u{
one a;
two b;
};
class three{
int kind;
u object;
public:
//commands here
};
int main() {
return 0;
}I have these three classes and the union. And I want to modify class 'three' to dynamically contain an object of any of the other classes 'one' and 'two'. For example if I have an object named "test" of class "three", and supposing that I have set it's kind to be 1 (which would point at class "one") I want to call it's function "show()" without having to check each time if the kind is '1 or '0'. I hope you understand what I mean. I know I could include in the 'public' some 'if's' that will check the 'kind' and call each object's respective function, but my actual program (the one I give you is a simple program that serves only the purposes of my quuestion) is so big that this would be so complicated to do. I am thinking that pointers and references would do the job, but I can't think of any way of doing it. Can someone alter the third class to make the program do such a thing? Thank you in advance.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
It looks like this is a job for an abstract base class:
class Base {
public:
abstract void set() = 0;
abstract void show() = 0;
};
class one : public Base {
int a;
};
class two : public Base {
float a;
}; |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
hm... I will try that and will see if I can do it. Thanks.
QUESTION 2: While I was thinking about the problem, another similar question came to my mind: I have this code: class three{
two a;
one b;
public:
void *ptr;
};
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
class three{
two a;
one b;
public:
one *ptr;
three()
{
ptr=&b;
}
};
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
I shouls have been more clear. I meant dynamically changing the objects. I mean to create a pointer which can point in to objects of class either "one" or "two" in the "three" class.
Anyway, I'm posting the entire program, since not asking specifically what I want is not going to get me anywhere. //This presents a class which holds an int.
//The class contains functions that will apply the
//int's binary objects into a vector, thus making it
//easy to apply bitwise operations to the int. After
//that, the int is updated to contain the vector's
//representation of an integer.
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class int_bit{
vector <int> v; // the vector to hold the bits of x
unsigned long x;
public:
void show();
unsigned long int retInt(){return x;}
int_bit(unsigned long a){x = a;}
void updateVector(); //update the vector's contents
void updateInt(); //update the integer's contents
void EraseVector(){v.clear();} //erase the vector
int retbit(int pos); //return the bit into the 'pos' position
void changeBit(int pos); //change the bit into the 'pos' position
};
void int_bit::show(){
int i=sizeof(long)*8-1;
long maska;
cout << "The integer bits are:\n";
for(;i>=0;i--){
maska=x & (1<<i);
cout << ((maska > 0)? "1 ":"0 ");
}
cout << "\nAnd the vector [" << v.size() << "] bits are:\n";
for(i=0; i<v.size(); i++){
cout << v[i] << " ";
}
}
//update the contents of the vector to match the
//bits of the integer in the class
void int_bit::updateVector(){
int i=sizeof(long)*8-1;
long maska;
for(; i>=0; i--){
maska = x & (1<<i);
if(maska == 0)
v.push_back(0);
else
v.push_back(1);
}
}
//returns the bit into the desired location
int int_bit::retbit(int pos){
return v[pos];
}
//**change the bit. If it's 1, make it 0. Otherwise, make it 1
void int_bit::changeBit(int pos){
v[pos] = ((v[pos] > 0)? 0 : 1);
}
//**update the int in the class with the changes we have made in
//**the vector
void int_bit::updateInt(){
vector<int> v2; //temporary vector
int i;
unsigned long int sum = 0;
for(i=(v.size()-1); i>=0; i--){
v2.push_back(v[i]);
}
for(i=0; i<v2.size(); i++){
sum = sum + (v2[i] * pow(2,i));
}
x = sum;
}1)To make this class work with floating point numbers 2)To make another class with the same functions as this class, only that the private members will be a "float x;" and "vector<float> v" elements. In both occasions, I want to rewrite as less code as possible. I don't want to entirely create another class for the float conversions. I also don't want to include many 'if' in the classes (in case you tell me to make another vector that can hold floating point numbers in the existing class and change the functions accordingly). I'm not asking you to write the code, only to tell me how could I do it. Note that I am not a proffessional (which is obvious I think) ![]() Please note that I made the program for practice only, and I don't thing it would be useful in anyone (since bitwise operations can be performed with the AND and OR operators). EDIT: Damn. I haven't thought that bitwise operations cannot be performed in floats. Anyway, the question still remains since my problem is not located into bitwise operators, but to the structure of the program.
__________________
Project::Soulstorm (personal homepage) Last edited by Soulstorm; Mar 12th, 2006 at 11:36 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|