Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
beginnner

What is the structure of a class implementation in C++?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Structure of a C++ Class

A class is user-defined datatype which has data members and member functions. In C++, classes are divided into the following three sections:

1. Class Declaration Section
2. Member Function Section
3. Main Function Section



class Animal{
   string animalName;
   public:
   Animal(string name);
   void animalSound();
   void printName();
};





Member Function Section

  • Member functions are methods used to access data members of the class.
  • Definition of member functions can be inside the class or outside the class.
  • This section contains definition of the member functions that are declared inside the class.




int main(){
   Animal a("Dog");
   a.printName();
   a.animalSound();
}

Class Declaration Section

  • You can declare class using the keyword class
  • You can add data members and member functions inside the class.
  • Accessing the data members and member functions depends solely on the access modifiers, i.e., public, private or protected.
  • A class always starts with { and ends with };



void Animal :: 
   Animal(string name){
      animalName = name;
   }
   void Animal :: animalSound(){
      cout << "Woof Woof!" << endl;
   }
   void Animal :: printName(){
      cout << animalName << endl;
   }

Main Function Section

  • Execution of the code starts from main.
  • In this section, we can create an object of a class.
  • We can call the functions defined inside the class.
#include <iostream> // Header files
using namespace std;
class Animal{ // class name
string animalName; // class data member
public:
Animal(string name); // Animal constructor
void animalSound(); // Member function declaration
void printName();
};
Animal :: Animal(string name){ // function defintion
animalName = name;
}
void Animal :: animalSound(){
cout << "Woof Woof!" << endl;
}
void Animal :: printName(){
cout << animalName << endl;
}
int main(){
Animal a("Dog"); // creating object of class Animal
a.printName(); // calling member function
a.animalSound(); // calling member function
}

RELATED TAGS

c++
beginnner
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring