I need to make the initializer of a class invisible for the child classes, but visible for the main(). How can I do it? The only way I see is to make it PUBLIC, but it will be inherited to the child classes. Any ideas?
You can make it private and add main as a friend
class A {
private:
A() {}
public:
friend int main(void);
};
int main(void) {
// Your code
}
Making the initializer private (cannot access the initializer even in main), there are many design patterns you can follow for solving your problem. One of the design pattern is Singelton.
The example is given below:
#include <iostream>
using namespace std;
class A{
private:
static A *single_instance;
//private constructor, cannot be inherited
A(){
}
public:
//for getting access in main
static A *getInstance() {
if (single_instance==NULL){
single_instance = new A();
}
return single_instance;
}
void print() {
cout<<"I'm from A";
}
};
//initializing with NULL
A *A ::single_instance=NULL;
class B:A {
//not access to the constructor
//but access to getInstance()
};
int main()
{
//now you can access it in main
A *obj = obj->getInstance();
obj->print();
return 0;
}
Note that, this design makes sure, only one instance can be created from your class.
Related
Assuming I have these classes (question marks mark the question what I need to pass here):
class A
{
...
public:
void pass()
{
B ins;
ins.doSth(?????);
}
};
class B
{
...
public:
void doSth(const A &sth)
{
...
}
}
int main()
{
A te;
te.pass();
}
Can you pass an instance of your own class or is this just an example of a failed class structure on my side?
The current object in a member function is *this. You can pass that to another function.
You will have to consider how the classes depend on each other, and that one class cannot use the other class until the declaration is complete.
This would work though:
class A
{
//...
public:
void pass();
};
class B
{
//...
public:
void doSth(const A &sth)
{
//...
}
};
// Here both classes are completely declared
void A::pass()
{
B ins;
ins.doSth(*this);
}
int main()
{
A te;
te.pass();
}
Your class "contains" an instance of each other so you'll face an error of undeclared types. To solve this issue you need to use forward declaration.
And you'll face another problem:
If your methods doSth() and pass() are defined inlinlely then you'll face a problem: "using incomplete types". The workaround this is to implement these methods outside the class so that each object has been fully constructed before used.
The program may look like:
class A;
class B;
class A{
public:
void pass();
};
class B{
public:
void doSth(const A &sth){
}
};
void A::pass(){
B ins;
ins.doSth(*this);
}
int main(){
A te;
te.pass();
return 0;
}
class Class1 {
Class1() { Class2::counter++;}
}
class Class2: public Class1 {
public:
static int counter;
}
The above is an example of what I am trying to do. I am trying to use a static variable of a subclass in the superclass but that gives me a compiler error since Class1 is initialized first and when the compiler reaches the Class2::counter++; it has not initialized Class2 yet and therefore I get a "Class2 has not been declared" error. How can I fix this? Do I need to change my logic completely?
From a OOP perspective a parent class is not able to access to a child field or property, it should the opposite way a child accessing to a parent field or property. I'm wondering whyeyou need to access tora child field from the parent? If you provide more details what you are trying to achieve the best solution will be provided by someone.
You could just pass a reference to your counter to the base class:
class Class1 {
public:
Class1(int& counter) { ++counter; }
};
class Class2: public Class1 {
public:
static int counter;
Class2() : Class1(counter) { }
};
int Class2::counter = 0;
You may put definition of constructor Class1 after Class2 definition:
class Class1 {
public:
Class1();
};
class Class2: public Class1 {
public:
static int counter;
};
int Class2::counter = 0;
Class1::Class1() { Class2::counter++;}
Live demo
A rule of thumb: base classes don't access child class members or methods.
If the base class needs something from a child class, declare an abstract virtual function in the base class for the child to implement.
Or move the common data members or methods into the base class.
In your case:
class Base
{
virtual void increment_counter(void) = 0;
void my_method(void)
{
increment_counter();
}
};
class Child : public Base
{
int counter;
void increment_counter(void)
{
++counter;
}
};
Simply define the constructor of class Class1 after the definition of Class2.
class Class1 {
public:
Class1();
}
class Class2: public Class1 {
public:
static int counter;
}
Class1::Class1() { Class2::counter++;}
Do not forget to define the static data member of Class2.:)
If you want to get freaky with templates, this sounds a possible use for the curiously recurring template pattern. For example:
template <typename T>
class CRTBase
{
CRTBase() { T::counter++; }
};
class CRTDerived : public CRTBase<CRTDerived>
{
public:
static int counter;
};
int CRTDerived::counter = 0;
In main, I want to access the display function. Here, in class B I declared class A as friend. So i thought that it is possible to access the private member functions.
But i dont know how to do that.
#include<stdio.h>
class A
{
public:
class B
{
public:
friend class A;
private:
void display()
{
printf("\nHi");
}
};
};
int main()
{
//here i wanna access display function.. is it possible?
return 1;
}
friend specifies what has access to private members. In your case, you want to access private members in the main function, so you should specify that it's friend:
class A
{
public:
class B
{
friend int main();
void display()
{
printf("\nHi");
}
};
};
int main()
{
// here you can access display function:
A::B object;
object.display();
}
Alternatively, if you want to make class A (and not anything else) a friend, then class A should access the display function. Any member of class A can do it:
class A
{
public:
class B
{
friend class A;
void display()
{
printf("\nHi");
}
};
// here you can access display function:
void access_display(B object)
{
object.display();
}
};
int main()
{
A object1;
A::B object2;
object1.access_display(object2);
}
I'm trying to get the following code to work, but I can't find good-enough documentation on how C++ handles public vs. private inheritance to allow me to do what I want. If someone could explain why I can't access Parent::setSize(int) or Parent::size using private inheritance or Parent::size using public inheritance. To solve this, to I need a getSize() and setSize() method in Parent?
class Parent {
private:
int size;
public:
void setSize(int s);
};
void Parent::setSize(int s) {
size = s;
}
class Child : private Parent {
private:
int c;
public:
void print();
};
void Child::print() {
cout << size << endl;
}
int main() {
Child child;
child.setSize(4);
child.print();
return 0;
}
Change Parent to:
protected: int size;
If you want to access the size member from a derived class, but not from outside the class, then you want protected.
Change Child to:
class Child: public Parent
When you say class Child: private Parent, you are saying it should be a secret that Child is a Parent. Your main code makes it clear that you want Child to be manipulated as a Parent, so it should be public inheritance.
When you use private inheritance, all public and protected members of the base class become private in the derived class. In your example, setSize becomes private in Child, so you can't call it from main.
Also, size is already private in Parent. Once declared private, a member always remains private to the base class regardless of the type of inheritance.
You cannot access private data-members of other classes. If you want to access private attributes of a superclas, you should do so via public or a protected accessors. As for the rest, see #casablanca's answer.
#include <iostream>
using namespace std;
class Parent {
private:
int size;
public:
void setSize(int s);
int fun() //member function from base class to access the protected variable
{
return size;
}
};
void Parent::setSize(int s) {
size = s;
}
class Child : private Parent {
private:
int c;
public:
void print();
using Parent::setSize; //new code line
using Parent::fun; //new code line
};
void Child::print() {
cout << fun() << endl;
}
int main() {
Child child;
child.setSize(4);
child.print();
return 0;
}
My question might not be too correct... What I mean is:
class MyClass
{
public:
MyClass()
{
}
virtual void Event()
{
}
};
class FirstClass : public MyClass
{
string a; // I'm not even sure where to declare this...
public:
FirstClass()
{
}
virtual void Event()
{
a = "Hello"; // This is the variable that I wish to pass to the other class.
}
};
class SecondClass : public MyClass
{
public:
SecondClass()
{
}
virtual void Event()
{
if (a == "Hello")
cout << "This is what I wanted.";
}
};
I hope that this makes at least a little sense...
Edit: _This changed to a.
What you need to do is make SecondClass inherit from FirstClass and declare _This as protected.
class FirstClass : public MyClass
{
protected:
string _This;
public:
and
class SecondClass : public FirstClass
What you got doesn't make sense because classes can only see members and functions from their parents (MyClass in your case). Just because two class inherit from the same parent does not mean they have any relation or know anything about each other.
Also, protected means that all classes that inherit from this class will be able to see its members, but nobody else.
I guess that you need something like this (for a sake of simplicity, I've omitted all the unnecessary code):
class Base{
public:
~Base(){}
protected:
static int m_shared;
};
int Base::m_shared = -1;
class A : public Base{
public:
void Event(){
m_shared = 0;
}
};
class B : public Base{
public:
void Event(){
if (m_shared == 0) {
m_shared = 1;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
a.Event();
b.Event();
return 0;
}
To explain above, I'll explain the static data members:
Non-static members are unique per class instance and you can't share them between class instances. On the other side, static members are shared by all instances of the class.
p.s. I suggest that you read this book (especially Observer pattern). Also note that above code is not thread-safe.