I'm doing some Arduino programming and I was trying to add functions to an existing class by creating my own class and inheriting the existing one. However, I'm not sure how to go about using class members from the child class within the constructor for the parent class... or if this is even possible. Below is my pathetic attempt to convey my question in code.
If it helps, I'm trying to make a child class for the Keypad class that will include some extra functions without having to rewrite the parent constructor for the child class.
class foo
{
public:
foo();
foo(int one, int two);
~foo();
};
class bar : public foo
{
public:
bar();
~bar();
private:
staticconstint dummy;
staticconstint dummy1;
};
foo::foo(int one, int two)
{
// do stuff with "one" and "two"
}
bar::bar()
{
foo(dummy, dummy1);
}
Thanks in advance, and just tell me if I'm not making any sense at all.
#include <iostream>
#include <string>
class Book {
public :
Book(const std::string _title) {title = _title;}
std::string title;
};
class PetersBook : public Book {//not the greatest example, but whatever.
public :
PetersBook(const std::string _title = "This is Peter's Book") : Book(_title) {}//default argument, and initialization list
};
int main(int argc, char* argv[]) {
PetersBook pbook;
std::cout << pbook.title << std::endl;
pbook = PetersBook("This is still Peter's Book");
std::cout << pbook.title << std::endl;
std::cin.get();
return 0;
}
Couldn't really think of a perfect, simple example.
class foo
{
public:
foo();
foo(int one, int two);
~foo();
};
class bar : public foo
{
public:
bar();
~bar();
private:
staticconstint dummy;
staticconstint dummy1;
};
foo::foo(int one, int two)
{
// do stuff with "one" and "two"
}
bar::bar()
: foo(dummy, dummy1)
{
}