Class
A Class is the building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
Syntax
Example:
The below example demonstrates the use of class in C++.
CPP
// C++ program to demonstrate // Class #include <bits/stdc++.h> using namespace std; class Geeks { // Access specifier public : // Data Members string geekname; // Member Functions() void printname() { cout << "Geekname is: " << geekname; } }; int main() { // Declare an object of class geeks Geeks obj1; // accessing data member obj1.geekname = "w3wiki" ; // accessing member function obj1.printname(); return 0; } |
Geekname is: w3wiki
Explanation: The above program defines a class named “Geeks” with a geekname attribute and a function printname() to print the geek’s name. In the main function, it creates an object named obj1, sets the geekname as “w3wiki”, and calls the printname() function to display it.
User Defined Data Types in C++
Data types are means to identify the type of data and associated operations of handling it. improve. In C++ datatypes are used to declare the variable. There are three types of data types:
- Pre-defined DataTypes
- Derived Data Types
- User-defined DataTypes
In this article, the User-Defined DataType is explained:
Contact Us