Hello, I'm trying to understand some things about derived classes: how to add something "extra" to existing functions in base class. My base class is "shape2D" like a rectangle, 3 functions: set values, calculate, show results. No problem here. Then I create a derived class named "shape3D". Please note the private stuff in the base class. Everything goes OK when I create one object of shape2D, do stuff, then another object of shape3D and do the same stuff. I get no errors for the private variables from the base class. Then I want to add another dimension (height) to the …

Member Avatar
Member Avatar
+0 forum 6

I have the following structure: CObject { protected: char *mName; public: CObject(char *n) { mName=strdup(n); } }; CVector:public CObject { char *mValues[50]; int mElements; public: CVector(char *n):CObject(n) {} }; CMatrix:public CObject { char *mValues[50][50]; int mLines; int mColumns; public: CMatrix(char *n):CObject(n) {} }; My main function: int main() { pV=new CVector("Vector 1"); pM=new CMatrice("Matrice 1"); delete pV; delete pM; } As you can see, i want to explicitly call them.

Member Avatar
Member Avatar
+0 forum 12

I have to update a road tunnel lighting program I wrote for a company. They now want a new standard to be applied, but with the option of switching between standards real time. And there will be other standards in the future. Currently the single object which calculates the lighting curves along the length of the tunnel is a CStd2003 m_Std ; object in the application. My immediate idea was that I could have a CStd base class and then a pointer rather than an object in the application: CStd* m_pStd ; // Will be either CStd2003 or CStd2011 Then …

Member Avatar
Member Avatar
+0 forum 3

How can I cast a base class to a derived on at runtime. What I am trying to do is the following: I need a system which will hold the subscriptions, where a certain type of message, has an assigned Subscriber. When a message is received, it will bi forwarded to this system, which will do a lookup if there are any subscribers to this kind of message and if so, message is forwarded to subscriber. I have an abstract base class called Response, followed by an abstract layers eg. AFResponse : Response, and then there are the actual (concrete) …

Member Avatar
Member Avatar
+0 forum 1

Hey all, I've been programming an ISBN system of sorts and I'm a little stuck (again). I want to output my linked list to a text file, so I setup the code like this: [code] void saveList(char fileName[40]) { for ( list< Publication* >::iterator it = pubList.begin(); it != pubList.end(); it++) { try{ std::ofstream ouf(fileName); if( !ouf ) throw std::ios::failure( "File Error" ); ouf<<"Publication,"<<(*it)->getPublisher()<<","<<(*it)->getIsbn(); ouf.close(); }catch( const std::exception& e){ std::cerr <<e.what()<<endl; } } }[/code] Now saving like this works alright with a small issue. I can only access the public functions of the base class object, I can't access the …

Member Avatar
Member Avatar
+0 forum 8

Hey all, I'm attempting to use an STL List to store a list of objects. Now I realise it's quite trivial to store a list of objects of my own definition. My real question is, how do I fill the list with objects that inherit from a base class. E.g. I have a class called Book which has the derived class Publication. How would I get the list to accept the base class aswell as derived classes? currently I am doing it something like this: [CODE] list<Book> objectList; //define list Book temp("Title", "ISBN"); Publication pubTemp("Title", "ISBN", "Publisher"); objectList.push_back(temp); objectList.push_back(pubTemp); [/CODE] …

Member Avatar
Member Avatar
+0 forum 4

#include<iostream> using namespace std; class area { double dim1, dim2; public: void setarea(double d1, double d2) { dim1= d1; dim2 = d2; } void getdim(double &d1, double &d2) { d1 = dim1; d2 = dim2; } virtual double getarea() { cout<<"You must override this functionn"; return 0.0; } }; class rectangle : public area { public: double getarea() { double d1, d2; getdim(d1, d2); return d1*d2;; } }; class triangle : public area { public: double getarea() { double d1, d2; getdim(d1, d2); return 0.5*d1*d2; } }; int main() { area *p; rectangle r; triangle t; r.setarea(3.3, 4.5); t.setarea(4.0, 5.0); …

Member Avatar
Member Avatar
+0 forum 7

Hey there, I was wondering if anyone had the time to look over my code and give me some tips or tweaks to help me get it running. The program is all about inheritance, where i have the derived class using the base class clockType (with using strings) to add time zones (MST, CST, EST, and PST<--as the default zone to start) to the two clocks in the main function. I am also trying to compare the values of clock1 and clock2 with the times zones to see if they are equal or not. The times for the clock can …

Member Avatar
Member Avatar
+0 forum 3

Hi, I have a list of base types which stores multiple derived types. The following code shows a simplified setup. [CODE]#include "Base.h" #include "DerivedA.h" #include "DerivedB.h" #include <iostream> #include <list> void main() { std::list<Base*> base; std::list<Base*> *basePointer = &base; for (int i = 0; i < 10; i++) { basePointer->push_back(new DerivedA); basePointer->push_back(new DerivedB); } }[/CODE] I iterate through this list using the iterator below: [CODE] std::list<Base*>::iterator baseItt = basePointer->begin(); baseItt++; [/CODE] Is there a way I can find out which derived type 'baseItt' is pointing to? I need this to help me with saving and loading in my program. Please …

Member Avatar
Member Avatar
+0 forum 7

Hello all I have a query,may be silly but i have to clear it.IT IS REGARDING THE UPCASTING.I want to "when we can invoke the methods of base class using the object of derived class.then what is the need of assigning the subclass object to base class reference." I mean what is advantages of using that reference in comparison to using subclass object.

Member Avatar
Member Avatar
+0 forum 5

The End.