Class in C++ ?
Aclass is a way to bind the data and it's associate functions together . it allows the data ( and fnctions ) to be hidden , if necessary , from external use . When defining a class we are creating a new abstract data type that can be treated like any other built - or data type . Generally , a class specification has two part .
- Class declaration
- Class functions definitions
The Class declare describe the data type and scope of it's memeber . The class function defines and describe how the class function are implemented .
The general form of class declaration is :
Class class_name
{
private :
variable declaration ;
Fuction declearation ;
Public :
variable declaration ;
Fuction declearation ;
};
Example of Class
#include <iostream>
using namespace std;
class ClassA {
public:
void set_a(int val);
int get_a(void);
private:
int a;
};
int ClassA::get_a(void) {
return a;
}
void ClassA::set_a(int val) {
a = val;
}
int main() {
ClassA a;
a.set_a(20);
cout << "Value of a is: " << a.get_a();
return 0;
}
Example of Class
Value of a is : 20
The Class is similiar to Struct Declaration , the keyword of class specifies , follows of abstract data type class_name. The body of class is inclosed within braces and terminate by the semicolon . The class body declare of variable and functions . These functions and variable are collectively called class memeber .
Ways of class memeber declared
- Private :
- Public :
- Protected :
Private :
private member work as it's name priavate(save) . It can access only accesss within a class it;s can not accessed outside the class and private member can use the data hiding in private in OOPS concept .
Public :
Public class memeber is open classs member which data can be access by outside the class .
Protected :
Protected class is also a type of class in c++ . A member declare in protected class is accessed by it's function and the other class is only derived it . It can not accessed by these two classes .
Source Code
#include<iostream>
using namespace std;
class student
{
Private ;
a = 10;
Public :
b = 20 ;
protected :
c = 30 ;
};
Objects in C++
When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax :
Class name Object_name ;
The declaration of an object is similiar to that of a variable of any basic type . The necessory memory space is allocated to an object at this stage . Note that , class specification , like a structure , provides only a template and does not create any memory space for the object .
Example of Object
#include <iostream>
using namespace std;
class new
{
// Access specifier
public:
// Data Members
string newname;
// Member Functions()
void printname()
{
cout << "newname is: " << newname;
}
};
int main() {
// Declare an object of class geeks
new obj1;
// accessing data member
obj1.newname = "abuzar ";
// accessing member function
obj1.printname();
return 0;
}
Output
New name is : Abuzar
This article is contributed by Mohd Abuzar . if you like code with abuzar and would like to contribute you can also write an article or mail at abuzar.abuzar.mohd325@gmail.com .
Please write comment if you find anything incorrect .
0 Comments
Do not Comment and any spam text