c++ classes and objects
C++ classes and objects
In this article, you will learn about the classes and objects in the c++ language in some interesting way to understand the concept of classes and objects.
Class is the user-defined data type with user-defined functionalities that are enabled by creating the object of the class.
Why we need a class?
Suppose an example of the student, the student has a name, roll number, and university name, etc. All the properties must be attached to the student class like qualification, marking. This is an example also taken in understanding the structure concept.
Difference between class and structure
- There is only one difference between structure and class is class gives privacy to the user-defined data type, but the structure does not give privacy to the user.
- The classes provide the functionality to the data type but the behaviour or operation does not allow in the structure.
How privacy define in the class
The new thing adds in the classes is the access specifiers which are defined the privacy of the class.
The access specifiers contain three keywords private, protected, and public. These access specifiers give access rights to the members of the class.
- The private access specifier allows access of the members within the same class or from their friend.we can not access private member from the outside of the class.
- The protected access specifier allows access of the members within the same class or form their friend but also accessible from their derived classes.
- The public members are accessible from anywhere like members of the structure.
By default,all the members of the class are private members.If you want to change the accessible then write access specifier protected or public before the members.
How to create a class
class keyword is used for creating the class in c++.The following is the syntax of the class:
class classname{
//access specifiers
//variables
//functions
}
What are c++ objects :
Declaring the variables of the user-defined data(class) the same as declaring the variables of the primitive data type. Actually, these are objects of the class .
classname objectname;
class student{
//class data
}
int main(){
student Ali, Jhonny, Krishnan;
}
Here , in the above code snippet, three objects(Ali, Jhonny, Krishnan) of the class student are declared in the main function.
It is not compulsory to create the object of the class in the main function,you can declare the object anywhere (in any function or within class or any other class).
How to access the data members and members of the class
Same as structure,we can access the data members or members function of the class by dot(.)operators.
For example1:
#include<iostream>
using namespace std;
class cricle{
public:
int radius;
int area()
{
int ar;
ar=(3.14)*(radius)*(radius);
return ar;
}
int diameter(){
int daim;
daim=radius+radius;
return daim;
}
};
int main()
{
cricle c1,c2;
c1.radius=5;
c2.radius=10;
cout<<"area of the cricle 1 = "<<c1.area()<<endl;
cout<<"diameter cricle 1 = "<<c1.diameter()<<endl;
cout<<"area of the cricle 2 ="<<c2.area()<<endl;
cout<<"diameter cricle 2 = "<<c2.diameter()<<endl;
return 0;
}
Output:
area of the cricle 1 = 78
diameter cricle 1 = 10
area of the cricle 2 =314
diameter cricle 2 =20
Example2:
#include<iostream>
using namespace std;
class cricle{
private:
int radius;
public:
cricle(int a)
{
radius=a;
}
int area()
{
int ar;
ar=(3.14)*(radius)*(radius);
return ar;
}
int diameter(){
int daim;
daim=radius+radius;
return daim;
}
};
int main()
{
cricle c1(5),c2(15);
cout<<"area of the cricle 1 = "<<c1.area()<<endl;
cout<<"diameter cricle 1 = "<<c1.diameter()<<endl;
cout<<"area of the cricle 2 ="<<c2.area()<<endl;
cout<<"diameter cricle 2 = "<<c2.diameter()<<endl;
return 0;
}
Output:
area of the cricle 1 = 78
diameter cricle 1 = 10
area of the cricle 2 =706
diameter cricle 2 =30
Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.
Comments
Post a Comment