开发者

How to call a function which belongs to a class in C++?

开发者 https://www.devze.com 2023-02-18 02:16 出处:网络
I am trying to call the function 开发者_如何转开发hello which belongs to the class program1 #include <iostream>

I am trying to call the function 开发者_如何转开发hello which belongs to the class program1

#include <iostream>
using namespace std;

class program1
{
    program1();
    ~program1();
    hello();
}

program1::hello()
{ 
    cout<<"hello";
}

int main()
{
    program1.hello(); //Call it like a normal function...
    cin.get();
}


Names inside a class are private by default.

class program1 {
public:
  program1();
  ~program1();
  void hello() ;
};

// ...

int main(int, char **) {
  program1 myProgram;
  myProgram.hello();
  return 0;
}

Alternatively, you can invoke a method on a temporary:

int main(int, char **) {
    program1().hello();
    return 0;
}

but that's probably for later in the semester.


you forgot to create an object:

int main()
{
program1 p1;
p1.hello();
}


  • Class definition should end with ;
  • Secondly, you need to instantiate class to call members on it. ( i.e., creation of an object for the class )
  • In C++, methods should have return type.

    program1::hello(); // method should have a return type.

  • class members and methods are private by default, which means you cannot access them outside the class-scope.


So, the class definition should be -

class program1
{
    public:       // Correction 1
      program1();
      ~program1();
      void hello();  // Correction 2
}; 

void program1::hello()  // Need to place return type here too.
{ 
   cout<<"hello";
}

Now on creation of object for class program1, it's method hello() can be called on it.


This version is edited. (make sure you include all the body of the methods)

#include <iostream>
using namespace std;

class program1
{
public: // To allow outer access
    program1();
    ~program1();
    void hello(); // The void
}; // Don't miss this semicolon

void program1::hello() // The void
{ 
    cout<<"hello";
}

int main()
{
    program1 prog; // Declare an obj
    prog.hello(); //Call it like a normal function...
    cin.get();
}

I noticed that you left out return type for your hello() function. If you want to call hello() as a member function, then as suggested you should create an object to it.

program1 prog;
prog.hello();

If you want to call it without an object, the you should use static function.

class program1
{
public: // To allow outer access
    program1();
    ~program1();
    static void hello(); // The void
}

then you can call it this way:

program1::hello();

Therefore the working code should be this way:

#include <iostream>
using namespace std;

class program1 {
public:
    void hello();
}; // Don't miss this semicolon

class program2 {
public:
    void static hello(); // to demonstrate static function
}; // Don't miss this semicolon

void program1::hello() {
    cout << "Hello, I'm program1." << endl;
}

void program2::hello() {
    cout << "Hello, I'm program2." << endl;
}

int main(void) {
    program1 prog1;

    prog1.hello(); // Non-static function requires object
    program2::hello(); // Static function doesn't

    return 0; // Return 0
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号