• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Classes » What is Class in C++
Next →
← Prev

What is Class in C++

By Dinesh Thakur

A class in C++ is a user-defined data type that binds data and the functions that operate on the data together in a single unit. Like other user-defined data types, it also needs to be defined before using its objects in the program. A class definition specifies a new data type that can be treated as a built-in data type.

The syntax for defining a C++ class is

class class_name {
  private:
    variables;
    functions;
  public:
    variables;
    functions;
  protected:
    variables;
    functions;
} ;

where,

class, private, public, protected = C++ keywords

class_name = the name of the class

variables = variables (data) of the class

functions = functions of the class

The variables and functions declared within the curly braces are collectively known as members of the class. The variables declared in the class are known as data members, while the functions declared in the class are known as member functions.

The keywords private, public and protected are known as access Specifiers (also known as visibility mode). Each member of a class is associated with an access Specifiers. The access Specifiers of a member controls its accessibility as well as determines the part of the program that can directly access the member of the class. When a member is declared private, it can be accessed only inside the class while a public member is accessible both inside and outside the class. Protected members are accessible both inside the class in which they are declared as well as inside the derived classes of the same class.

Once an access specifier has been used, it remains in effect until another access specifier is encountered or the end of the class declaration is reached. An access specifier is provided by writing the appropriate keyword (private, public or protected) followed by a colon ‘ : ‘ . Note that the default access specifier of the members of a class is private. That is, if no access specifier is provided in the class definition, the access specifier is considered to be private.

To understand the concept of defining a class, consider this example.

Example: A simple class definition

class book {
  //private by default
  char title [30] ; //variables declaration
  float price;
  public:
   void getdata(char [],float); //function declaration
   void putdata ();
} ;

In this example, a class named book with two data members title and price and two member functions getdata() and putdata() is created. As no access specifier is provided for data members, they are private by default, whereas, the member functions are declared as public. It implies that the data members are accessible only through the member functions while the member functions can be accessed anywhere in the program.

Generally, data members are declared as private and member functions are declared as public. Declaring the data members as private hides them from the rest of the program. This safeguards the data members and prevents any accidental changes to them by other parts of the program, thereby, implementing the concept of data hiding of object-oriented programming. Similarly, specifying the member functions as public, provides an interface that is visible and accessible to the other parts of the program.

Note that it is useful to declare the member functions as private if they are to be accessed only within other member functions of the same class and not outside the class. In addition, all the members (data as well as functions) of a class can be declared as private. However, such a class prevents its access from the outside world and does not serve any purpose.

You’ll also like:

  1. Write A C++ Program To Make A Call Class Constructor Or Not During Class Array Declaration.
  2. What is Virtual Class and Friend Class
  3. Write A C++ Program To Declare The Local Class. Local Class And Local Objects.
  4. Write A C++ Program For Using Class Pointer And Class Array Together.
  5. Write A C++ Program To Define Inner Class
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW