Download How to create the code behind the class in cpp programming using Microsoft Visual Sytudio.mp4
Creating the code behind a class in C++ using Microsoft Visual Studio is a common practice to separate class declarations (in header files) from definitions (in source files) — improving readability and maintainability.
Here’s a step-by-step guide 👇
🧩 Step 1: Create a New Project
-
Open Microsoft Visual Studio.
-
Go to File → New → Project.
-
Choose Console App (C++).
-
Name your project (e.g.,
ClassExample) and click Create.
🧱 Step 2: Add a Header File (.h)
-
In Solution Explorer, right-click the project → Add → New Item...
-
Select Header File (.h) and name it, e.g.,
Student.h. -
Add the class declaration here.
Example – Student.h
#pragma once
#include <string>
class Student {
private:
std::string name;
int age;
public:
Student(std::string n, int a); // Constructor declaration
void displayInfo(); // Method declaration
};
⚙️ Step 3: Add a Source File (.cpp)
-
Right-click the project again → Add → New Item...
-
Choose C++ File (.cpp) and name it
Student.cpp. -
Include the header file and define the methods.
Example – Student.cpp
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student(std::string n, int a) {
name = n;
age = a;
}
void Student::displayInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
🚀 Step 4: Use the Class in main.cpp
Visual Studio automatically creates a main.cpp file. Use your class there:
Example – main.cpp
#include <iostream>
#include "Student.h"
using namespace std;
int main() {
Student s1("Alice", 21);
s1.displayInfo();
return 0;
}
🧠 Step 5: Build and Run
-
Press Ctrl + F5 or click Run → Start Without Debugging
-
You’ll see:
Name: Alice, Age: 21
✅ Summary
| File | Purpose |
|---|---|
Student.h |
Class declaration (what it looks like) |
Student.cpp |
Class definition (how it works) |
main.cpp |
Entry point of the program |
Enjoy! Follow us for more...























