What are member and what are the method in my code.

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Hi All,

I am Just learning c++ about classes and trying to understand the difference between member and method

My code : I have written my own code

Code:
#include <iostream>

#include <string>

using namespace std;

//A class can be created before main() is called.

class person
{
    //The access labels Public, Protected and Private are used within classes to set access permissions for the members in that section of the class
  public:
    string name;
    int age;
};

int main()
{
  //Create an object of person and  then creating separate objects for the two person like this:
  person person1;
  person person2;
 
  person1.name = "sunil";
  person1.age = 15;

  person2.name = "Gajya";
  person2.age = 16;

  cout << person1.name << ": " << person1.age << endl;
  cout << person2.name << ": " << person2.age << endl;

  return 0;
}
When I compile code it shows detail name and age of person 1 and person 2

I don't understand what are member and what are the method in my code.

any help will appreciate

Thanks & warm regards
Gajya Madake
 

Papabravo

Joined Feb 24, 2006
21,159
A member of a class is a piece of data. A method of a class is a function or procedure operates on member of the class and potentially other data as well.
 

BobTPH

Joined Jun 5, 2013
8,809
Yes. And your class has no methods.

A method is a function declared within the class. Here is your class with a method added:class person{

Code:
   //The access labels Public, Protected and Private are used within classes to set access permissions for the members in that section of the class  public:
    string name;
    int age;
    void setName(string newname)
    {
        name = newname;
    }
};
You could then use the method like this:

Code:
    person p;
    p.setName("Bob");
Bob
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Yes. And your class has no methods.

A method is a function declared within the class. Here is your class with a method added:class person{

Bob
Thank you Bob

This code has method

Code:
#include <iostream>

#include <string>

using namespace std;


class person
{
    //The access labels Public, Protected and Private are used within classes to set access permissions for the members in that section of the class
  public:
    string name;
    int age;
    void setName( )
     {
        cout << "Bob";
     }
};

int main() {
   
    person p;
    p.setName();
   
  return 0;
}
 

402DF855

Joined Feb 9, 2013
271
"Method" isn't really valid C++ terminology. What you probably mean is a "member function". (See The Annotated C++ Reference Manual p. 174).

Members are defined for classes and may be data, functions, enumerations, bitfields, friends and type names. (p. 169-170.)
 
Top