1

I am new to C++, and confused about how does a class access a public method in another class in C++. For example,

//.h of class A
class A {
public:
  void setDimension (int width, int height);
  A* obj;
}

//.cpp of class A
#include "A.h"
void A::setDimension (int width, int height) {
    // do some stuffs here
}

//.h of class B
#include "A.h"
class B {
public:
    void function ();
   //do something here
}

//.cpp of class B
#include "A.h"
#include "B.h"
void B::function() {
     obj->setDimension(int width, int height);
}

And now I want the class B can access the public method "setDimension" in class A. I think the dependency files are included, but when I run the program, I got an error says that setDimension was not declared in this scope. How can I call the setDimension method inside class B. Many thanks!

Xiufen Xu
  • 463
  • 3
  • 6
  • 11
  • In your example **it won't**. setDimension() is an instance method then you need an object... – Adriano Repetti Nov 14 '16 at 11:02
  • You need a class `A` instance to call its methods. Or `setDimension` should be static. – Ari0nhh Nov 14 '16 at 11:02
  • 4
    Take a look at [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/4115625) – Danh Nov 14 '16 at 11:03
  • 2
    firstly create an object from A class. `A a; a.setDimension(3, 5)` – saygins Nov 14 '16 at 11:04
  • 1
    Welcome to SO, please check out [The definitive C++ book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There's little point in answering the q without pointing out that you need to look into OOP. – George Nov 14 '16 at 11:04
  • Note: with every answer given so far, the object of `A` will be murdered straight after calling `setDimension()` which i'd imagine renders the function pointless. – George Nov 14 '16 at 11:06
  • you nneed and instance of A in B, so you can call the public methods offered by A – ΦXocę 웃 Пepeúpa ツ Nov 14 '16 at 11:11

3 Answers3

5

You have first to create an instance of object A and then call setDimension on this instance.

 //.cpp of class B
#include "A.h"
#include "B.h"
void B::function() {
      A myInstance;
      myInstance.setDimension(10, 10);
}

Or you need to declare the method as static and can call it without instantiation:

//.h of class A
class A {
   public:
     static void setDimension (int width, int height);
}

 //.cpp of class B
#include "A.h"
#include "B.h"
void B::function() {
     A::setDimension(10, 10);
}

If Class A is abstract:

//.h of class B
#include "A.h"
class B : A {
public:
    void function ();
}

//.cpp of class B
#include "A.h"
#include "B.h"
void B::function() {
     this->setDimension(10, 10);
}
Casius
  • 83
  • 6
  • @Casius. Hi Casius, can I use the pointer, which I added in my question, to call the setDimension method? – Xiufen Xu Nov 14 '16 at 11:28
  • @George yes, sorry, happens due to language hopping. You need to use the scope resolution operator :: to call the static method. – Casius Nov 14 '16 at 11:46
  • @Xiufen Xu You didn't get the conecpt of OOP yet. If you want to use a pointer you have to create it in your function in B like A* obj; obj->setDimension(10, 10) – Casius Nov 14 '16 at 11:49
  • I am sorry that I forget to clarify the class A is an abstract class, so I think we cannot instantiate an object for A. – Xiufen Xu Nov 14 '16 at 16:20
  • If A is abstract, you need to inherit it. Either in a third Class C or you can extend Class B. – Casius Nov 14 '16 at 16:31
  • @XiufenXu `this->` is redundant here, the only reason to call a function inherited from an abstract class using a pointer is generally to have the pointer in another class entirely. (There are exceptions with static and globals, although in all honesty I think you just need to get some good learning material), and as a side note, as of C++11, the scenarios where you'll want to use a so called naked pointer are few and far between. – George Nov 14 '16 at 16:59
  • @Casius. Got you! Thank you so much! – Xiufen Xu Nov 14 '16 at 17:00
  • @George Your suggestions and materials are really helpful! Thank you so much! – Xiufen Xu Nov 14 '16 at 17:03
0

You need to create an A (and choose a particular width and height, or pass those in from somewhere) so that you can use its method

void B::function() {
   A mya;
   int mywidth = 10;
   int myheight = 20;
   mya.setDimension(mywidth, myheight);
}
Arthur Tacca
  • 8,833
  • 2
  • 31
  • 49
0

You can declare the method setDimension(int width,int height); in class A as static.

static void setDimension(int width,int height);

void B::function(){

    A::setDimension()

}

You access static member functions using the class name and the scope resolution operator ::

George
  • 2,101
  • 1
  • 13
  • 27
JPX
  • 93
  • 1
  • 4