Can i call a class on another class.

ex.

class a {

public: void add (int,int) ...

//maybe call class b here...///
};

class b{

public : void mult(int,int)...
};

something like this.

class a{
   public:
      void someFunction(){
             std::cout << "hello";
          }
};
class b{
    public:
       a myClass;
};

int main(void){
    b aClass;
    b.myClass.someFunction();
    std::cin.get();

    return 0;
}

Or is inheritance what you are after?

Chris

thanks chris

If I use things from another class I tend to just do a heirarchy, branching one off another and protecting the values i want only in the heirarchy, for example

class Mother{
};

class Son: Mother   //Mom class Births Son Class :P
{
};

It's not a class called, you call a member function for an object of another class. So if you can declare a variable of the class (as a member, for example) then you can call member functions of this variable class.

Now let's remember: you must declare an entity before use it. In the original post snippet you can't declare any class B variables in the class A definition: the class B is not defined yet. Therefore you can't declare class B variables as class A members and can't call class B member functions in class A definition.

None the less you can use class B in class A definition:

// File classes.h
class B; // incomplete type declaration

class A { // We don't know class B members here
public:
    // Declare references or pointers to B only
    void fA(const B& b) const;
    const char* name() const { return "A"; }
};
// No problems with class A in class B definition
class B {
public:
    void fB(const A& a) const;
    const char* name() const { return "B"; }
};

void TestAB();
// End of classes.h file

// file classes.cpp
#include "classes.h"
// Now all classes are declared properly
void A::fA(const B& b) const
{
    std::cout << name() << '(' << b.name() << ')' << std::endl;
}

void B::fB(const A& a) const 
{
    std::cout << name() << '(' << a.name() << ')' << std::endl;
    a.fA(*this); 
}

void TestAB()
{
    A   a;
    B   b;
    b.fB(a);
}

Try this and see what happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.