Cpp Classes

Cpp Classes

A class in C++ is a user defined type or data structure declared with keyword class that has data and functions
(also called methods) as its members whose access is governed by the three access specifiers private
protected or public (by default access to members of a class is private). The private members are not accessible outside the class;
they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.
outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.
Instances of a class data type are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.

Definition Of Class

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.

Syntax

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

Simple Class Example Program

#include 
#include
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;
}

Output

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


                      << PREVIEW >>




                      <<  NEXT    >>

Comments