Yes, SubClass is derived from ParentClass.
the "string Name" and "string Adress" are in the ParentClass, while "int Phone" is in the SubClass.
How would I use pointers to point directly to an object in the class? and should I make another array to save those pointers in?
In the final programme, I will be writing the arrays to a file and save that file and not to the screen (just mentioning the full scenario), but I assume the process is pretty much the same.
The full code is as follows:
ParentClass.h:
Code:
class ParentClass
protected:
ParentClass(); // constructor
ParentClass(string Name, string Adress); // overloaded constructor!
virtual string GetName() const { return itsName; } // inline constant funktion
virtual void SaveName(string Name) { itsName = Name; } // inline function
virtual string GetAdress() const {return itsAdress; } // inline constantfunction
virtual void SaveAdress(string Adress) { itsAdress = Adress; } // inline function
private:
string itsName;
string itsAdress;
// overloaded ParentClass
ParentClass::ParentClass(string Name, string Adress): itsName(Name), itsAdress(Adress)
{
cout << "ParentClass constructor...\n";
}
SubClass.h:
Code:
// inheritance from ParentClass
class SubClass: virtual public ParentClass {
public:
SubClass();
SubClass(string Name, string Adress, int Phone); // overloaded constructor
virtual ~SubClass() { std::cout << "SubClass destructor...\n"; } // destructor!
virtual string GetPhone() const { return itsPhone; }
private:
int itsPhone;
};
// overloaded SubClass
SubClass::SubClass(string Name, string Adress, int Phone):
ParentClass(Name, Adress),
itsPhone(Phone)
{
cout << "SubClass constructor...\n";
}
and finally main.ccp:
Code:
#include <iostream>
#include <string>
#include "ParentClass.h"
#include "SubClass.h"
using std::string;
int main() {
// Array of objects to store in file.
ParentClass people[ ] = {
SubClass("Name", "Adress", Phone)
};
std::cout << people;
(I've changed the names of the classes to English, so it's easier to read)m so if there is some syntaxes in names, don't mind it, it's not there in my programme)