×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

Create a class with inline functions in C++

By IncludeHelp Last updated : November 04, 2023

A class in C++ may have the inline function as member functions.

Problem statement

Write a C++ program to create a class with inline functions.

Steps to create a class with inline functions in C++

  • Define a class with some data members
  • Create an inline constructor function to initialize the data members of the class
  • Create inline member functions
  • Inside the main function, create an object to the class and then access the inline functions

C++ program to create a class with inline functions

Consider the below example to create class with inline functions in C++.

#include <iostream>
using namespace std;

// defining class
class Student {
 private:
  // data members
  int english, math, science;

 public:
  // inline constructor function
  Student(int english, int math, int science) {
    this->english = english;
    this->math = math;
    this->science = science;
  }
  // inline member function - to return total marks
  inline int getTotalMarks() { 
      return english + math + science; 
  }
  
  // inline member function - to return total marks
  inline float getPercentage() { 
      return ((float)getTotalMarks() / 300 * 100); 
  }
};

int main() {
  // Create object to the Student class
  Student std(65, 90, 92);

  // calculate and display the
  // total marks and Percentage
  cout << "Total marks: " << std.getTotalMarks() << endl;
  cout << "Percentage: " << std.getPercentage() << endl;

  return 0;
}

Output

Total marks: 247
Percentage: 82.3333

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.