Why am I not able to access a public member when inheriting? - c++

#include <iostream>
using namespace std;
class BaseClass
{
private:
int privateInt;
protected:
int protectedInt;
public:
int publicInt;
};
class ChildClass : public BaseClass
{
publicInt = 0;
};
int main()
{
ChildClass c;
cout << c.publicInt;
}
As you can see, ChildClass is inheriting from BaseClass. publicInt is a public member of BaseClass. So why do I get an error when I try to assign a value to publicInt in ChildClass, and also when trying to print the value in main?

The assignment is simply misplaced. Instead of trying to assign a value to publicInt in the class definition (where you should only put member declarations or type aliases), you can do so e.g. in the constructor of ChildClass (or any other member function):
class ChildClass : public BaseClass
{
public:
ChildClass() {
publicInt = 0;
}
};
This resolves the compiler error for the cout statement, too.

Related

Why is the inherited function inaccessible?

A small example to reproduce the error in Visual Studio (Community) 2022:
#include <iostream>
#include <memory>
class Base {
public:
virtual void b();
protected:
std::string Method() {
return "Base::Method";
}
};
class SubA : public Base {
public:
// Prevent compiler from optimizing class away.
int doSomething() {
return 7;
}
};
int main() {
SubA sa;
sa.doSomething();
sa.Method(); // <-- Function is inaccessible.
}
Why is the error? The inheritance is public and I'm calling Method on an object from the SubA class.
The inheritance is public
But the member function is protected. You're accessing the member function from main which is outside of the scope of the class or its decendants, so it has no access to protected member functions.

Inheritance of pure virtual function

Consider the following code:
#include <iostream>
using namespace std;
class Parent{
virtual void print() = 0;
};
class Derived : public Parent {};
int main() {
Derived d1;
return 0;
}
My doubt over here is that, print function in the parent class is a private member function, then how does it get inherited in the derived class?

How to use a member variable in a base class function when its size is only known to the derived class

I want to implement a function in a baseclass that uses members of its derived classes. So with each DerivedClass, I would have a member of a different value.
Here's an example. The BaseClass has member function Foo() which uses variable arrStr. This content of this NUL terminated char array however is only to be found in a derived class.
How can I make BaseClass "know" the variable arrStr without knowing its size? Is that even possible?
class BaseClass
{
public:
BaseClass();
~BaseClass();
protected:
void Foo()
{
prtinf("%s\n", arrStr);
};
};
class DerivedClass : public BaseClass
{
public:
DerivedClass();
~DerivedClass();
protected:
char arrStr[] = "FooString!";
};
Add (pure) virtual access functions in your base class which you implement in the derived class.
#include <iostream>
#include <string>
class BaseClass
{
protected:
void Foo()
{
std::cout << GetString() << std::endl;
}
private:
virtual std::string GetString() const = 0;
};
class DerivedClass : public BaseClass
{
private:
virtual std::string GetString() const override
{
return "FooString!";
}
};
Live demo
How can I make BaseClass "know" the variable arrStr without knowing its size? Is that even possible?
You insert it as a parameter when calling Foo:
class BaseClass
{
public:
virtual void Process() = 0;
protected:
void Foo(char* str) // <-- value inserted here as parameter
{
prtinf("%s\n", arrStr);
}
};
class DerivedClass : public BaseClass
{
public:
void Process() override
{
Foo(arrStr); // call in derived class, with derived value
}
protected:
char arrStr[] = "FooString!";
};
Client code:
BaseClass * b = new DerivedClass;
b->Process(); // call Foo(arrStr) internally

Tell compiler about subclass -polymorphism

If I have a baseclass Base and a subclass Sub, and in the subclass have a memberfunction that doesnt exist in the superclass - how do I tell the compiler its there?
#include <iostream>
using namespace std;
class Base {
public:
};
class Sub : public Base {
public:
void printFromSub() {
cout << "I am not inherited ;-)" << endl;
}
};
int main() {
Sub sub;
Base* base;
base = ⊂
base->printFromSub(); // not possible at compile-time
return 0;
}
You have to cast to derived class.
If you are sure that base points to an object of the derived class, then you can use static_cast.
static_cast<Sub*>(base)->printFromSub();
If you aren't sure, then you'll need a runtime check.
Sub* p = dynamic_cast<Sub*>(base);
if (p) p->printFromSub();

Access protected data members of the base class from the derived class

I have a base class and the derived class. I need to access the protected member of the base class in the derived class. However, Eclipse does not allow me to access the data member as if it were a member of a derived class without caring that it was inherited. How do I do that?
class BaseClass {
protected:
static int a;
int b;
}
class DerivedClass: public BaseClass {
void SomeMethod {
a=10; // cannot resolve symbol
b=10; // cannot resolve symbol
BaseClass::a=10; //does not complain
BaseClass::b=10; //does not complain
}
}
I couldn't completely understand your question, but fixing the syntax errors, the following should work:
class BaseClass {
protected:
static int a;
int b;
}; // <-- Missing semicolon
int BaseClass::a = 0; // Define static member
class DerivedClass: public BaseClass {
void SomeMethod() { // <-- Missing ()
a=10;
b=10;
}
};// <-- Missing semicolon