Skip to main content

Class in C++

Class 

class:

  • Group of Similar kinds of objects with same behavior and attributes is known as class.
  • As a class, it combined the data attributes and the behavior attributes of an object together, and formed an object which can simulate both aspects of a real-world object.
  • Class can be defined by using class keyword.

Syntax:

    class className
    {
        data members; 
         member function();
    };

Data Members: 

Data Members are the Variables of the class.

Syntax:

    access Specifier :
                data type varName;

Member Functions

Member functions are the Functions of the class.

Syntax:

    access Specifier:
            return type functionName()
            {
                Statements;
            

Access Specifiers: 

Access Specifiers defines the accessibility of the variable and function.
There are 3 Types of Access Specifiers in C++

1. public
2. private
3. protected

1.public Access Specifier:

This define the accessibility of the Data members and member function that they can be accessed out side the class.

2. private Access Specifier:

It defines the data can be accessed within a class only.

3. protected Access Specifier:

It defines the data can be accessed by the inherited class. 

How to access data from the class? 

To Access data from the class we use .(dot) or Member Operator with the Object of the class.

How to Create an Object of the Class?
            ClassName objName;

Class Naming Convention:

  • Class Name Starts with an Alphabet.
  • Class Name can be Alphanumeric. 
  • Class Name can not contain any special character except (_) underscore.
  • Underscore can be used for combining two words of the class name.
  • Class Name should starts with capital letter. 
Ex: Write a Program to print the Student name and roll-number using class

#include <iostream>
using namespace std;
class Student
{
          int rollNo;
          string name;
          
    public:
            void setData()
            {
                cout<<"Enter the Roll Number :";
                cin>>rollNo;
                cout<<"Enter the name :";
                cin>>name;
            }
            void getData()
            {
                cout<<"\n Welcome "<<name<<" your Roll Number is "<<rollNo;
            }
    
};

int main()
{
    Student s;
    s.setData();
    s.getData();
    return 0;
}
 

Comments

Popular posts from this blog

Programming Basics

Logic Building Data_Types Operators Algorithms Pseudo_Code Hello Guys, Let's Play with Logic.........but wait, before that you must know that how to build Logic in Programming as you all know that programming is all about Logic..... So Let's start the game... If you are new Here that means you are absolutely right Place, Or If you already know, than you will learn new things here! If we Talk about the Programming we should be familiar with  these terms..... 1. Variable 2. Data Types 3.Keywords Variable: Variable is something which holds the value.                  Like:      a=10;                 Here,                 a---------------------->  Variable                  10 ---------------------> Value * The Value of Variable can be changed , *  ...

FAQs

Basic Questions on Programming Languages 1. What is the meaning of Programming? Ans: Programming is the process of Scheduling the task to perform step by step to Execute a specific Task. 2. Types of Programming Languages? Ans: Types:             Procedural Programming Language            Object Oriented Programming Language            Object Based Language            Scripting Language Procedural Programming Language   C Programming  Object Oriented Programming Language     C++     Java     C#     VB.Net Object Based Language     JavaScript Scripting Language     Python     VBScript     PHP     Perl     Ruby     R 3. Types Of Operators in Programming Languages. Ans:      Operators Symbols Type Example Assignment = U...