C++ Class and Functions


In this article, you will learn about C++ class and functions. You will learn about different ways of defining member functions of the class.

C++ class and functions

The functions associated with a class are called member functions of that class.

Member functions must be declared inside the class but they can be defined either inside the class or outside the class.

Different ways of defining member functions of a class

There are two ways in which the member functions can be defined :

  • Inside the class definition
  • Outside the class definition

C++ class and functions: Inside the class definition


As the name suggests, here the functions are defined inside the class.

Functions defined inside the class are treated as inline functions automatically if the function definition doesn’t contain looping statements or complex multiple line operations.

Example

#include <iostream>
using namespace std;
class car
{
  private:
    int car_number;
    char car_model[10];
  public:
    void getdata()
     {
       cout<<"Enter car number: "; cin>>car_number;
       cout<<"\n Enter car model: "; cin>>car_model;
     }
    void showdata()
     {
       cout<<"Car number is "<<car_number;
       cout<<"\n Car model is "<<car_model;
     }
};
// main function starts
int main()
 {
    car c1;
    c1.getdata();
    c1.showdata();
    return 0;
 }


Output

Enter car number : 9999
Enter car model : Sedan
Car number is 9999
Car model is Sedan

Here in above program getdata() and showdata() are the member function defined inside the class.

C++ class and functions: Outside the class definition


As the name suggests, here the functions are defined outside the class however they are declared inside the class.

Functions should be declared inside the class to bound it to the class and indicate it as it’s member but they can be defined outside of the class.

To define a function outside of a class, scope resolution operator :: is used.

Syntax for declaring function outside of class

class class_name
 {
   ........
   ........
   public:
     return_type function_name (args); //function declaration
 };
//function definition outside class
return_type class_name :: function_name (args)
 {
   ...........; // function definition
 }

Example

#include <iostream>
using namespace std;
class car
{
  private:
    int car_number;
    char car_model[10];
  public:
    void getdata(); //function declaration
    void showdata();
};
// function definition
void car::getdata()
 {
   cout<<"Enter car number: "; cin>>car_number;
   cout<<"\n Enter car model: "; cin>>car_model;
 }
void car::showdata()
 {
   cout<<"Car number is "<<car_number;
   cout<<"\n Car model is "<<car_model;
 }
// main function starts
int main()
 {
    car c1;
    c1.getdata();
    c1.showdata();
    return 0;
 }

Output

Enter car number : 9999
Enter car model : Sedan
Car number is 9999
Car model is Sedan

Here is this program, the functions showdata() and getdata() are declared inside the class and defined outside the class. This is achieved by using scope resolution operator ::.