void* is not a pointer object / casting implementation - c++

From my code below I'm trying to create a Course grading system. So, it will store the type of assignment, its weightage and the achieved grade. I'm trying to get it to print the raw score (which is the grade entered for example: "B-" or 85.50; depending on whether it is an exam or quiz).
I have read about "casting" which should resolve the issue of using void* as a pointer but I am still unclear as to how I can implement it. I tried static_cast<struct Grading*>(c.gs[0])(commented out in code) but it still returned the address of the value, instead of the value itself. Any help will be appreciated. Thanks in advance!
#include <iostream>
#include <vector>
using namespace std;
struct Grading{
string name;
int percentage;
virtual ~Grading(){}
virtual void* get_raw_score(){return 0;}
void* get_adj_score(){return 0;}
};
struct Quiz:public Grading{
string letter_grade;
Quiz(const string title, const int weight, const string grade){
name=title;
percentage=weight;
letter_grade=grade;
}
virtual void* get_raw_score(){return &letter_grade;}
};
struct Exam:public Grading{
double *score = new double;
Exam(const string title, const int weight, const double grade){
name=title;
percentage=weight;
*score=grade;
}
virtual void* get_raw_score(){return &score;}
};
struct Project:public Grading{
string letter_grade;
Project(const string title, const int weight, const string grade){
name=title;
percentage=weight;
letter_grade=grade;
}
virtual void* get_raw_score(){return &letter_grade;}
};
struct CourseWork{
vector<Grading*> gs;
void push_back(Grading* g){
gs.push_back(g);
}
void sort_name(){}
void sort_score(){}
};
ostream& operator<<(ostream& o,const CourseWork c){ //output the raw score here.
//static_cast<struct Grading*>(c.gs[0]);
o<<c.gs[0]->name<<endl<<c.gs[0]->percentage<<c.gs[0]->get_raw_score()<<endl;
return o;
}
int main() {
CourseWork c;
c.push_back(new Quiz("Quiz", 5, "B-"));
c.push_back(new Quiz("Quiz", 5, "C+"));
c.push_back(new Quiz("Quiz", 5, "A"));
c.push_back(new Exam("Midterm", 10, 50));
c.push_back(new Exam("Final", 30, 85.5));
c.push_back(new Project("Project", 5, "A-"));
c.push_back(new Project("Project", 15, "B-"));
c.push_back(new Project("Project", 15, "B-"));
c.push_back(new Project("Demo", 10, "C"));
cout << "** Showing populated data..." << endl;
cout << c << endl << endl;;
c.sort_name();
cout << "** Showing sorted by name..." << endl;
cout << c << endl << endl;
c.sort_score();
cout << "** Showing sorted by score..." << endl;
cout << c << endl;
return 0;
}

Instead of writing a function that returns a void* and trying to figure out what it actually returns you can make the function return a string in all cases but when it is a number just convert the number to a string. You could use:
struct Grading{
string name;
int percentage;
virtual ~Grading(){}
virtual std::string get_raw_score() = 0; //make pure virtual since we only use Grading as a base class
void* get_adj_score(){return 0;}
};
struct Quiz:public Grading{
string letter_grade;
Quiz(const string title, const int weight, const string grade){
name=title;
percentage=weight;
letter_grade=grade;
}
virtual std::string get_raw_score(){return letter_grade;}
};
struct Exam:public Grading{
double score; // this does not need to be a pointer
Exam(const string title, const int weight, const double grade){
name=title;
percentage=weight;
score=grade; // no more dereference
}
virtual std::string get_raw_score(){return std::stod(score);} // now the score is represented as a string
};
struct Project:public Grading{
string letter_grade;
Project(const string title, const int weight, const string grade){
name=title;
percentage=weight;
letter_grade=grade;
}
virtual std::string get_raw_score(){return letter_grade;}
};
struct CourseWork{
vector<Grading*> gs;
void push_back(Grading* g){
gs.push_back(g);
}
void sort_name(){}
void sort_score(){}
};
ostream& operator<<(ostream& o,const CourseWork c){ //output the raw score here.
//static_cast<struct Grading*>(c.gs[0]);
o<<c.gs[0]->name<<endl<<c.gs[0]->percentage<<c.gs[0]->get_raw_score()<<endl;
return o;
}

You can make your base class abstract and use a template parameter instead of using a void* pointer:
struct IGrading {
virtual const std::string get_raw_score() const = 0;
};
template<typename T>
class Grading : public IGrading {
public:
virtual ~Grading() {}
};
std::ostream& operator<<(std::ostream& os, const IGrading& g) {
os << g.get_raw_score();
return os;
}
and derive your implementations as needed to implement the interface contract
class Quiz : public Grading<std::string> {
public:
Quiz(std::string title, int weight, std::string rawScore)
: title_(title)
, weight_(weight).
, rawScore_(rawScore) {}
virtual const std::string& get_raw_score() const {
return rawScore_;
}
private:
std::string title_;
int weight_;
std::string rawScore_;
};
... and analogous implementations:
class Exam : public Grading<double> {
public:
Exam(std::string title, int weight, double rawScore)
: title_(title)
, weight_(weight)
, rawScore_(rawScore) {}
virtual const std::string& get_raw_score() const {
return std::to_string(rawScore_);
}
private:
double rawScore_;
};
Or have a generic base class implementation for numeric template parameters:
template<typename T>
class NumericGrading : public Grading<T> {
protected:
NumericGrading(T rawScore) : rawScore_(rawScore) {}
public:
virtual const std::string& get_raw_score() const {
return std::to_string(rawScore_);
}
protected:
T rawScore_;
};
and let Exam derive that:
class Exam : public NumericGrading<double> {
public:
Exam(std::string title, int weight, double rawScore)
: NumericGrading<double>(rawScore)
, title_(title)
, weight_(weight) {}
};
This will keep your code type safe and avoid any weird and error prone casting operations.
The CourseWork container can use the abstract interface:
struct CourseWork{
vector<IGrading*> gs;
void push_back(IGrading* g){
gs.push_back(g);
}
void sort_name(){}
void sort_score(){}
};

Related

Instantiation of Templates

Please see the following code:
// templateClassTemplate.cpp
#include <iostream>
class Account{
public:
explicit Account(double amount=0.0): balance(amount){}
void deposit(double amount){
balance+= amount;
}
void withdraw(double amount){
balance-= amount;
}
double getBalance() const{
return balance;
}
private:
double balance;
};
template <typename T, int N>
class Array{
public:
Array()= default;
int getSize() const;
private:
T elem[N];
};
template <typename T, int N>
int Array<T,N>::getSize() const {
return N;
}
int main(){
std::cout << std::endl;
Array<double,10> doubleArray;
std::cout << "doubleArray.getSize(): " << doubleArray.getSize() << std::endl;
Array<Account,1000> accountArray;
std::cout << "accountArray.getSize(): " << accountArray.getSize() << std::endl;
std::cout << std::endl;
}
This code is taken from a learning course on template initialisation.
I have two questions:
How is the object Array<double,10> doubleArray initialized since it is using a default constructor that takes no arguments?
How is the object Array<Account,1000> accountArray initialized?
The template is instantiated by replacing the type T in the template with the type given as the first argument, and the integer N with the value of the second argument.
The same thing happens when instantiating the definition of getSize.
That is, Array<double,10> doubleArray; works exactly the same as
class DoubleArray10{
public:
DoubleArray10()= default;
int getSize() const;
private:
double elem[10];
};
int DoubleArray10::getSize() const { return 10; }
DoubleArray10 doubleArray;
and Array<Account,1000> accountArray; works exactly like
class AccountArray1000{
public:
AccountArray1000()= default;
int getSize() const;
private:
Account elem[1000];
};
int AccountArray1000::getSize() const { return 1000; }
AccountArray1000 accountArray;

C++ I have some seryous issues with inheritance when derived and base class have different types of parameters, like shown below:

Im a newbie in c++ and recently discovered classes;
I learned about constructors, overloading operators, the rule of three and right now i tried to learn inheritance.
I created 4 classes: 2 parents, 2 childs, but i occured some problems in class parent1
This is class parent1:
class parent1{
protected:
float slr;
int age;
char *name;
void set_new_name(char ch[10001]);
public:
parent1()
{
slr=0.0;
age=0;
name=NULL;
}
parent1(char ch[10001], float sl, int ag)
{
slr=sl;
age=ag;
set_new_name(ch);
}
parent1(const parent1 &p1)
{
char temp[10001];
strcpy(temp,p1.name);
if(name != NULL)
delete[] name;
set_new_name(temp);
slr=p1.slr;
age=p1.age;
}
parent1 &operator=(const parent1 &p1)
{
/// same lines as in copy constructor above
return *this;
}
char* get_name() const;
void print1();
~parent1()
{
delete[] name;
}
};
This is his child class, child1:
class child1 : public parent1{
protected:
int id;
void set_id(int j);
public:
child1(): parent1()
{
set_id(0);
}
child1(char ch[10001],float sl, int ag, int j): parent1(ch,sl,ag)
{
set_id(j);
}
child1(const child1 &p2): parent1(p2)
{
set_id(p2.get_id());
}
child1 &operator=(const child1 &p2)
{
set_id(p2.get_id());
parent1::operator=(p2);
}
int get_id() const;
void print2();
};
There is class parent 2:
class parent2{
protected:
char *name1;
char *name2;
void set_new_name1(char ch1[10001]);
void set_new_name2(char ch2[14]);
public:
parent2()
{
name1=NULL;
name2=NULL;
}
parent2(char ch1[10001], char ch2[14])
{
set_new_name1(ch1);
set_new_name2(ch2);
}
parent2(const parent2 &p3)
{
char temp2[10001];
strcpy(temp2,p3.name1);
if(name1 !=NULL)
delete[] name1;
set_new_name1(temp2);
/// .. . same lines as above, this time for name2 and p3.name2
}
parent2 &operator=(const parent2 &p3)
{
/// .. same lines as in copy constructor above
return *this;
}
char* get_name1() const;
char* get_name2() const;
void print3();
~parent2()
{
delete[] name1;
delete[] name2;
}
};
And there is his child, child 2:
class child2: public parent2{
protected:
char *job;
void set_new_job(char ch3[15]);
public:
child2(): parent2()
{
job=NULL;
}
child2(char ch1[10001], char ch2[10001],char ch3[11]): parent2(ch1,ch2)
{
set_new_job(ch3);
}
child2(const child2 &p4): parent2(p4)
{
char temp6[11];
strcpy(temp6, p4.job);
if(job != NULL)
delete[] job;
set_new_job(temp6);
}
child2 &operator=(const child2 &p4)
{
/// same lines as in copy constructor
parent2::operator=(p4);
}
char* get_job() const;
void print4();
~child2()
{
delete[] job;
}
};
As u can see up here, class parent1 have 3 types of parameters ( one float, one int and one char*).
Nonte: set_ functions works ok, get_functions just return class parametes (also works ok) , print functions just print classes parameters ( ex: cout << name1; also works fine)
The problem is that this code refuse to work when i create the objects in main.
First i thought it is operator= being overloaded to many times, bit it turned out to be the float parameter from parent1
There is the main:
char ch[10001]="my name", ch1[10001]="my name 1", ch2[14]="my name 2", ch3[11]="some code";
int ag=10;
float sl=10.1;
parent1 o1;
o1=parent1(ch,sl,ag);
o1.print1();
parent1 o2(o1);
o2.print1();
child1 o3;
o3=child1(ch,sl,ag,3);
o3.print2();
child1 o4;
o4=child1(ch,sl,ag,6);
o4.print2();
o4=o3;
o4.print2();
parent2 o5;
o5=parent2(ch1,ch2);
o5.print3();
child2 o6(ch1,ch2,ch3);
o6.print4();
The only things that seems to make it run are:
deleting the float parameter from parent1;
deleting the last class ; (i really don't know why the last class affect the program)
creating the last object like this : child2 o6(ch1,ch2,ch3); , which is frustrating because it should work like the others;
I know the code i sheared is very long, but Please , Help me to understand what i need to do to solve this stupid bug !
I see at least 3 issues in the code that will lead to a crash/undefined behavior.
First:
parent1(const parent1 &p1)
{
char temp[10001];
strcpy(temp,p1.name);
if(name != NULL) // name isn't initialized yet,
delete[] name; // these 2 lines shouldn't be here
set_new_name(temp);
slr=p1.slr;
age=p1.age;
}
Second: (these ones are reported by the compiler when warnings are enabled)
child1 &operator=(const child1 &p2)
{
set_id(p2.get_id());
parent1::operator=(p2);
return *this; // this line is missing
}
Third:
child2 &operator=(const child2 &p4)
{
char temp7[11];
strcpy(temp7, p4.job);
if(job != NULL)
delete[] job;
set_new_job(temp7);
parent2::operator=(p4);
return *this; // this line is missing
}
The return statement is not "inherited". Each function that's supposed to return something must do so.
With these changes the code runs:
my name
my name
3
6
3
my name 1
my name 2
some code
(Live demo)
Some additional improvement notes:
An array like char ch[10001] can't really be a function argument in C++. When it's used as an argument it silently decays to char *. So you might as well replace all char ch[10001] with const char* ch (and better yet, std::string), to avoid confusion.
Also, there's no point in allocating a temp array. You can just directly do set_new_name(p1.name):
parent1(const parent1 &p1)
{
set_new_name(p1.name);
slr=p1.slr;
age=p1.age;
}
It would be prudent to invest some time in getting familiar with a Debugger. It's all but impossible to make a working application without debugging it. And enable compiler warnings. With GCC use -Wall -Wextra, with MSVC - /W4.
Here's an example of the code using std::string. Thanks to std::string we can follow the rule of 0:
class parent1 {
protected:
float slr = 0;
int age = 0;
string name;
void set_new_name(string const &ch) { name = ch; }
public:
parent1() {}
parent1(string const &name, float slr, int age)
: slr(slr), age(age), name(name) {}
string const &get_name() const { return name; }
void print1();
};
void parent1::print1() { cout << get_name() << '\n'; }
class child1 : public parent1 {
protected:
int id = 0;
void set_id(int j) { id = j; }
public:
child1() : parent1() {}
child1(string const &name, float sl, int ag, int j)
: parent1(name, sl, ag), id(j) {}
int get_id() const { return id; }
void print2();
};
void child1::print2() { cout << get_id() << '\n'; }
class parent2 {
protected:
string name1;
string name2;
void set_new_name1(string const &ch) { name1 = ch; }
void set_new_name2(string const &ch) { name2 = ch; }
public:
parent2() {}
parent2(string const &name1, string const &name2)
: name1(name1), name2(name2) {}
string const &get_name1() const { return name1; }
string const &get_name2() const { return name2; }
void print3();
};
void parent2::print3() {
cout << get_name1() << '\n';
cout << get_name2() << '\n';
}
class child2 : public parent2 {
protected:
string job;
void set_new_job(string const &ch) { job = ch; }
public:
child2() : parent2() {}
child2(string const &name1, string const &name2, string const &job)
: parent2(name1, name2), job(job) {}
string const &get_job() const { return job; }
void print4();
};
void child2::print4() { cout << get_job() << '\n'; }
And this works equally well.

Accessing list of fields and types in a class in c++

Hi i am trying to create a simple ORM in c++ for a project. For this example assuming a simple class as
class userProfile: public BaseOrm
{
public:
string username;
string email;
};
Now base orm has a method save() and migrate(). What i want is when a person calls migrate() all the schema , in this case username and email are populated as db tables and on save they persist on database.
What i am having problem with is how do i get what all fields are defined in the class, like in this example username and email and also there types, string in this case. Any help would be appreciated.
I know there is no reflection in c++, so i don't actually care about the variable name but more on the number of variables and there types to map them with DB.
adding reflection to c++ is not insanely difficult but it does require a reasonably good knowledge of template type deduction and some careful planning.
In this working example I have made a start for you. This framework supports writing the members out to a "statement" class (modelling a database prepared statement).
Similar techniques can be used to build out the SQL generation for CRUD.
No doubt there are already libraries that do this for you...
#include <iostream>
#include <iomanip>
#include <string>
#include <tuple>
#include <utility>
using namespace std;
struct statement
{
void setString(int index, const std::string& value)
{
std::cout << "setting index " << index << " to value " << std::quoted(value) << std::endl;
}
};
struct BaseOrm
{
virtual void serialise(statement& stmt) const = 0;
};
template<class Class>
struct class_tag {
using type = Class;
};
template<const char* Name>
struct name_tag {
static constexpr const char* name() { return Name; }
};
namespace detail {
struct reflection_item_concept
{
virtual const std::string& name() const = 0;
virtual std::string to_archive_string(const void* object) const = 0;
virtual void from_archive_string(void* object, const std::string& as) const = 0;
};
template<class T>
std::string to_archive_string_impl(const T& val) {
return std::to_string(val);
}
const std::string& to_archive_string_impl(const std::string& s) {
return s;
}
template<class NameTag, class Class, class Type>
struct reflection_item : reflection_item_concept
{
reflection_item(Type Class::* mfp) : mfp(mfp) {}
static const class_tag<Class> class_info() { return {}; };
static const char* raw_name() { return NameTag::name(); };
// concept implementation
const std::string& name() const override {
static const std::string s = raw_name();
return s;
}
std::string to_archive_string(const void* object) const override
{
auto& val = (*reinterpret_cast<const Class*>(object)).*mfp;
return to_archive_string_impl(val);
}
void from_archive_string(void* item, const std::string& as) const override
{
// similar mechanism here
}
Type Class::* mfp;
};
}
template<class NameTag, class Class, class Type>
constexpr auto reflection_item(NameTag, Type Class::* mp)
{
return detail::reflection_item<NameTag, Class, Type> { mp };
}
struct class_reflection_concept
{
virtual void serialise(const void* object, statement& stmt) const = 0;
};
namespace detail {
template<class ClassTag, class...ReflectionItems>
struct reflection_impl : class_reflection_concept
{
reflection_impl(ReflectionItems...refs)
: _reflectors(std::make_tuple(refs...))
{}
template<std::size_t...Is>
void serialise_impl(std::index_sequence<Is...>, const void* object,
statement& stmt) const
{
using expand = int[];
void(expand{
0,
(stmt.setString(Is + 1, std::get<Is>(_reflectors).to_archive_string(object)),0)...
});
}
void serialise(const void* object, statement& stmt) const override
{
serialise_impl(std::make_index_sequence<sizeof...(ReflectionItems)>(),
object, stmt);
}
std::tuple<ReflectionItems...> _reflectors;
};
}
template<class ClassTag, class...ReflectionItems>
auto& make_reflection(ClassTag tag, ReflectionItems...items)
{
static const detail::reflection_impl<ClassTag, ReflectionItems...> _ { items... };
return _;
}
const char txt_username[] = "username";
const char txt_email[] = "email";
const char txt_x[] = "x";
class userProfile: public BaseOrm
{
public:
string username = "test username";
string email = "noone#nowhere.com";
int x = 10;
// implement serialisation
void serialise(statement& stmt) const override
{
reflection.serialise(this, stmt);
}
static const class_reflection_concept& reflection;
};
const class_reflection_concept& userProfile::reflection =
make_reflection(class_tag<userProfile>(),
reflection_item(name_tag<txt_username>(), &userProfile::username),
reflection_item(name_tag<txt_email>(), &userProfile::email),
reflection_item(name_tag<txt_x>(), &userProfile::x));
int main()
{
userProfile x;
statement stmt;
x.serialise(stmt);
}
expected results:
setting index 1 to value "test username"
setting index 2 to value "noone#nowhere.com"
setting index 3 to value "10"
What I understand is that you want a generic behaviour for classes which have a variable set of fields.
I suggest you to create a "field" interface which will be stored in your base class with a container (for example a map of [fieldName, fieldInterface]). You still have to implement a behaviour for each field's type, but then you can create any class derived from the base class which have a dynamic set of field.
Here is an example :
#include <iostream>
#include <map>
using namespace std;
//the "Field" interface
class IFieldOrm
{
public:
virtual ~IFieldOrm() {}
virtual void save() = 0;
virtual void migrate() = 0;
};
//your base class
class BaseOrm
{
public:
virtual ~BaseOrm();
virtual void save();
virtual void migrate();
protected:
map<string, IFieldOrm*> m_fields; //prefer a smart pointer if you don't want to mess with raw pointer
};
//base class implementation
void BaseOrm::save()
{
for(auto& f : m_fields)
f.second->save();
}
void BaseOrm::migrate()
{
for(auto& f : m_fields)
f.second->migrate();
}
//don't forget to free your "fields" pointers if you have raw pointers
BaseOrm::~BaseOrm()
{
for(auto& f : m_fields)
delete f.second;
}
//then implement your basic types
//(like string, int, ..., whatever type you want to store in your database)
class StringFieldOrm : public IFieldOrm
{
public:
StringFieldOrm(const string& value) : m_value(value) {}
virtual void save();
virtual void migrate();
private:
string m_value;
};
void StringFieldOrm::save()
{
cout << "Save value " << m_value << endl;
//save stuff...
}
void StringFieldOrm::migrate()
{
cout << "Migrate value " << m_value << endl;
//migrate stuff...
}
class IntFieldOrm : public IFieldOrm
{
public:
IntFieldOrm(int& value) : m_value(value) {}
virtual void save();
virtual void migrate();
private:
int m_value;
};
void IntFieldOrm::save()
{
cout << "Save value " << m_value << endl;
//save stuff...
}
void IntFieldOrm::migrate()
{
cout << "Migrate value " << m_value << endl;
//migrate stuff
}
//and finally implement your final class
//note that this object can be "dynamically extended" by inserting new fields,
//you may want to prevent that and I can think of a solution if you want to
class UserProfile: public BaseOrm
{
public:
UserProfile(const string& username, const string& email, int age);
};
UserProfile::UserProfile(const string& username, const string& email, int age)
{
m_fields["username"] = new StringFieldOrm(username);
m_fields["email"] = new StringFieldOrm(email);
m_fields["age"] = new IntFieldOrm(age);
}
int main(int argc, char* argv[])
{
UserProfile user = UserProfile("Batman", "bw#batmail.com", 30);
user.save();
return 0;
}
create a userProfile variable and access them:
userProfile user;
int main(){
std::cout << user.username;
std::cout << user.email ;
}
this is how you would access them, except for different reasons, not printing them to the screen.

Copy constructor for derived class from base pointer

I have looked all around and can't find the answer to my question anywhere. I am trying to use a copy constructor of a derived class from a pointer array of base classes. The only thing I have learned is I should probably use dynamic_cast but cant get that working.
This is the important parts of my code so far (original is way to big since I have 16 different files but this should be enough).
EDIT: The error I receive doing it this way is |26|error: cannot dynamic_cast '& properties[0]' (of type 'class Property**') to type 'class Commercial*' (source is not a pointer to class)|
#include "rentals.h"
#include "commercial.h"
#include "sales.h"
#include "comSales.h"
#include "resSales.h"
#include "resRentals.h"
#include "comRentals.h"
const int MAX_PROPERTIES = 5;
int main(void) {
int i;
Property *properties[MAX_PROPERTIES];
properties[0] = new Commercial("Notting Hill McDonalds", "4 Gardiner Road",
"Notting Hill", 5000, "Li3000");
properties[1] = new ResRentals("Janet Dalgleish", "30 Firhill Court",
"Mary Hill", 4000, 500.00, 300.00, 4);
properties[2] = new Commercial(dynamic_cast<Commercial*>(properties[0])); // <-- the copy constructor I can not get to work.
delete[] properties;
return 0;
}
commercial.cpp file
#include "property_a.h"
#include "commercial.h"
Commercial::Commercial() : Property() {
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
license = "NULL";
}
Commercial::Commercial(string theOwner, string theAddress,
string theSuburb, int thepostCode,
string theLicense): Property(theOwner, theAddress,
theSuburb, thepostCode), license(theLicense) {}
Commercial::~Commercial() {}
Commercial::Commercial(const Commercial& orig) : Property(orig),
license(orig.getLicense()) {}
void Commercial::print() {
cout << getOwner() << endl;
cout << getAddress() << endl;
cout << getSuburb() << endl;
cout << getPostcode() << endl;
cout << getLicense() << endl;
}
commercial.h file
#ifndef __COMMERCIAL_H__
#define __COMMERCIAL_H__
#include "property_a.h"
class Commercial : public virtual Property
{
protected:
string license;
public:
Commercial();
Commercial(string theOwner, string theAddress, string theSuburb,
int thepostCode, string theLicense);
~Commercial() ;
Commercial(const Commercial& orig);
void input() ; // Data input for a Shop object
void print() ; // Data output for a Shop object
string getLicense() const {return license;}; //Note the use of const
void setLicense(string theLicense) {license = theLicense;};
};
property_a.cpp file
#include "property_a.h"
Property::Property(){
owner = "NULL";
address = "NULL";
suburb = "NULL";
postcode = 0;
}
Property::Property(string theOwner, string theAddress,
string theSuburb, int thepostCode):
owner(theOwner), address(theAddress),
suburb(theSuburb), postcode(thepostCode){}
Property::~Property() {}
Property::Property(const Property& orig) :
owner(orig.getOwner()), address(orig.getAddress()),
suburb(orig.getSuburb()), postcode(getPostcode()) {}
property_a.h file
#ifndef __PROPERTY_A_H__
#define __PROPERTY_A_H__
/*TODO REQUIRED HEADER FILES AND NAMESPACES*/
#include <string>
#include "utility1.h"
class Property
{
protected:
string owner;
string address;
string suburb;
int postcode;
public:
Property();
Property(string theOwner, string theAddress, string theSuburb, int thepostCode);
virtual ~Property();
Property(const Property& orig);
virtual void input() ; // Data input for a Property object
virtual void print() ; // Data output for a Property object
string getOwner() const {return owner;}; //Note the use of const
string getAddress() const {return address;};
string getSuburb() const {return suburb;};
int getPostcode() const {return postcode;};
void setOwner(string newOwner) {owner = newOwner;};
void setAddress(string newAddress) {address = newAddress;};
void setSuburb( string newSuburb) {suburb = newSuburb;};
void setPostcode(int newPostcode) {postcode = newPostcode;};
};
#endif
I hope this is enough details
properties[2] = new Commercial(dynamic_cast(properties[0])); // <-- the copy constructor I can not get to work.
This is casting properties[0] to Commercial*. But this isn't the signature of your copy constructor. Therefore, you need new Commercial(*dynamic_cast<Commercial*>(properties[0]));.
In this example you could use static_cast<Commercial&>(*properties[0]) since you know properties[0] is a Commercial type.
However, in general if you're using dynamic_cast it probably means you're not sure what the derived type is and you would need to check for NULL (i.e., the cast failed) before dereferencing.
Alternative
You could consider a polymorphic API to take care of this for you.
class Base
{
public:
virtual ~Base() = default;
Base* clone() const = 0;
};
class D1 : public Base
{
public:
virtual ~D1() override = default;
D1* clone() const { return new D1(*this); }
};
class D2 : public Base
{
public:
virtual ~D2() override = default;
D2* clone() const { return new D2(*this); }
};
int main()
{
std::unique_ptr<Base> b1(new D2());
std::unique_ptr<Base> b2(b1->clone());
return 0;
}
It would be nice to see the errors but it looks like you aren't calling the copy constructor at all:
new Commercial(dynamic_cast<Commercial*>(properties[0]));
is like calling
Commercial(Commercial * other);
so you need
new Commercial(*dynamic_cast<Commercial*>(properties[0]));

How to use a member function inherited from base class?

I have a derived class named TimeWithDate inherited from Date class and Time class.
I try to use the member function by using ::.
like this:
int subtract(TimeWithDate& other_date){
return Date::subtract(other_date) + Time::subtract(other_date);
}
but I got this warning:
Error: a nonstatic member reference must be relative to a specific object.
Then I tried this way:
int subtract(TimeWithDate& other_date){
return *(Date*)this.subtract(other_date) + *(Time*)this.subtract(other_date);
}
and got this warning:
Error: 'this' may only be used inside a nonstatic member function.
What should I do?
whole code
#include<iostream>
using namespace std;
class Time
{
int hour, second, minute;
public:
Time();
Time(int h, int m, int s);
void set(int h, int m, int s);
void increment();
void display();
bool equal(Time &other_time);
bool less_than(Time &other_time);
int subtract(Time &another);
};
class Date
{
int year, month, day;
public:
Date();
Date(int y, int m, int d);
void increment();
bool equal(Date &another);
int subtract(Time &another);
};
class TimeWithDate : public Time, public Date
{
public:
bool compare(TimeWithDate&);
void increment();
int subtract(TimeWithDate&);
};
bool TimeWithDate::compare(TimeWithDate &other_date){
if (Date::equal(other_date) && Time::equal(other_date))
return true;
else return false;
}
void TimeWithDate::increment(){
Time::increment();
Time zero(0, 0, 0);
if (Time::equal(zero))
Date::increment();
}
int subtract(TimeWithDate& other_date){
return Date::subtract(other_date) + Time::subtract(other_date);
}
subtract() should be a member function of class TimeWithDate. It appears that you have it as a non-member/ static function. So, this pointer is no more available in that function.
You need parse your whole code, below works fine in my computer(VS2012).
#include <iostream>
using namespace std;
class Base1
{
public:
void print(const char *str){ cout << "base1 " << str << endl; }
};
class Base2
{
public:
void print(const char *str){ cout << "base2 " << str << endl; }
};
class Derived : public Base1, public Base2
{
public:
void print(const char *str);
};
void Derived::print(const char *str)
{
cout << "Derived " << str << endl;
Base1::print(str);
Base2::print(str);
}
int main()
{
Derived d;
d.print("hello");
return 0;
}