Skip to main content

Simple Class Example Program In C++

2 min read Updated June 30, 2026
Share:
On this page (8sections)

About this program

This is an example program in c c class example programs. Read the concept first: Introduction of Classes (OOP), then study the code and output below.

Definition

A class is a blueprint, or prototype which defines and describes the member attributes and member functions.

The C++ programming language allows programmers to separate program-specific datatypes through the use of classes. Classes define types of data structures and the functions that operate on those data structures. Instances of these datatypes are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.

Syntactically, classes are extensions of the C struct, which cannot contain functions or overloaded operators.

for better understanding,

Class Syntax

class classname {
    Access - Specifier :
    Member Varibale Declaration;
    Member Function Declaration;
}

Simple Class Example Program

/*  Example Program Simple Class Example Program In C++
    little drops @ thiyagaraaj.com

    Coded By:THIYAGARAAJ MP                             */

// Header Files
#include <iostream>
#include<conio.h>
using namespace std;

// Class Declaration

class person {
    //Access - Specifier
public:

    //Variable Declaration
    string name;
    int number;
};

//Main Function

int main() {
    // Object Creation For Class
    person obj;

    //Get Input Values For Object Varibales
    cout << "Enter the Name :";
    cin >> obj.name;

    cout << "Enter the Number :";
    cin >> obj.number;

    //Show the Output
    cout << obj.name << ": " << obj.number << endl;

    getch();
    return 0;
}

Sample Output

Enter the Name :Byron
Enter the Number :100
Byron: 100

Learn the concept first, then study the code:

Frequently Asked Questions

What does this C++ program do?
It is a C++ example program that demonstrates Simple Class, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses reading user input, illustrating a common pattern in C++ programming.

Related Tutorials

Search tutorials