Â
What are Classes ?
Class is the basic concept of OOP's. Now here's a question,Â
What is OOP?
-> OOP stands for Object Oriented Programing.
-> Object Oriented Programming is about to creating objects that contains both data(variables) and functions.
-> In OOP Class and Objects are main aspects.
Advantages of OOP:-
1. It is faster and easier to execute programs with Classes and Objects.
2. OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug.
3. OOP makes it possible to create full reusable applications with less code and shorter development time.
4. It provides a clear structure to the program.
Definition of Class:-
--> Class is a User-defined datatype . A class has some attributes and some functions. These are called as members of class. It works as a 'blueprint' for class objects.
-->Example:-
--> In Programming terms attributes are called as variables and functions are called as functions only.
-->In computer science, an object can be a variable, a data structure, a function, or a method, and as such, is a value in memory referenced by an identifier.
-->While declaring a class a "class" keyword is to be used and while declaring member functions and variables inside the class some access specifiers such as "public", "private", "protected" need to be specified.
-->By default the access specifier for all members is "private". Which means all members of class can be accessed only by class members not by other members which outside of the class.Â
--> Objects can contain data and code:Â
data in the form of fields (often known as attributes or properties or variables), and code, in the form of procedures (often known as methods or functions).
Syntax to Declare Class in C++ :-
#include<iostream>
using namespace std;
class class_name //class declaration
{
private: // access specifier
// member variables or member functions
// for example
// member variable
public: // access specifier
// member function
// Here we will create all the functions required in the program
};
// main function
int main()
{
// calling the class as datatype of obj variable
// calling a class
class_name variable;
return 0;
}
Syntax to create Objects of Class (calling class in main function) :-
Example :-
//****HELP FOR CODERS****
//This program is a example of Class declaration and calling of class with object.
//Example is given in addition of three numbers.
#include<iostream>
using namespace std;
class Class1
{
private:
int a, b, c, sum=0;
public:
void get_number()
{
cout<<"Enter the first number\n";
cin>>a;
cout<<"Enter the second number\n";
cin>>b;
}
int get_third_number()
{
cout<<"Enter the third number\n";
cin>>c;
return c;
}
int calculate()
{
sum = a+b+c;
return sum;
}
void show_output()
{
cout<<"The sum of three numbers is: "<<sum;
}
};
int main()
{
Class1 obj;
obj.get_number();
obj.get_third_number();
obj.calculate();
obj.show_output();
return 0;
}
Output
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP