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 type varName;
return type functionName()
{
Statements;
}
1. public
2. private
3. protected
};
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++
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;
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
Post a Comment
If you have any doubts, Please let me Know