×
BLUE
cpp: Classes and objects


cpp: Classes and objects
  1 June 2021
Read more Like this Post.
Classes is block of code its user defined data type like structures. classes have block of code which use in different places by calling the class objects. Class contain variables, functions, structures, and etc.
Calling of the class object first we need to make a instance of the class. When a class is created we can call it in different places in the program. In a class functions, variable and other stuff that’s called objects of the class.

Example code:

#include 
#include 
using namespace std;
// section 1
class _person_{
    public:
        char name[35];   
};

int main(){
    // section 2
    _person_ person;
    // section 3
    strcpy(person.name, "zohaib khan");
    // section 4
    cout << person.name;
}
This is simple program with a class. First in section 1 we made class by using class keyword and set the class name is _person_. In section 2 we made a object of the class like a data type and a variable name. In section 3 calling the class object by using dot ( . ) operator and copying the value on it.

Access modifiers

In class have three access modifier.
  • public
  • Private
  • Protected

Public:

When this modifier called the object from below of the modifier is accessible all the program outer of the class and inside of the class.

Private:

after setting private modifier the data can not be accessible outside of the class.

Protected

Protected data can not be accessible outside the class however they can accessible in some inherited classes.
Default modifier is private mean if user not specify the modifier in c default modifier automatically set is private and object is not access outside of the class.

Example of Modifiers

#include 
#include 
using namespace std;
// section 1
class myClass{
    private:
        int private_value = 24;
    public:
        char public_value = 34;   
};

int main(){
   myClass _cls_;
   cout << _cls_.public_value << endl;
   cout << _cls_.private_value << endl;
}
the error of the code says the data of the private is not available on main function the scoop.

constructors

the constructor is a method which is automatically called when a class object is created. Constructor and class name to be same to same.

Example:

#include 
#include 
using namespace std;
// section 1
class myClass{
    public:
        myClass(){
            cout << "this is constructor output";
        }
};

int main(){
   myClass _cls_;
}
In the example we create a class with name of myClass and a constructor with same name with parenthesis. When the class object is created in main function the constructor will be automatically invoked.

output of the code:
this is constructor output
constructor can get parameters we can pass parameters in class object like in example

Example:

#include 
#include 
using namespace std;
// section 1
class adder{
    public:
    // section 2
        adder(int x, int y){
            cout << x   y;
        }
};

int main(){
   adder sum(4, 8);
}
In section 1 creating a class name adder and this class have constructor which take two arguments and print the sum of the variables. In main function we create a object name ‘sum’ of the class and pass the values as parameters.

Destructors

The destructors is used for destroy all object. Its used for clean up the memory its called automatically when a class object goes out of range or scope. Its called when a variable deleted or when a program is ended the destructor called automatically the destructors and class name to be same to same. The destructor name followed by a tilde( ~ ). Destructors not accept any parameters . Destructors not have any returning value.

Example:

#include 
using namespace std;
// section 1
class _class_{
    private: int val, val1;
    public:
        // section 2
        _class_(int x, int y){
            cout << "Constructor " << endl;
            val = x; val1 = y;
        }
        // section 3
        void add(){
            cout << val   val1 << endl;
        }
        // section 4
        ~_class_(){
            cout << "Destructor " << endl;
        }
};

int main(){
    // section 5
   _class_ cls(4, 8);
    // section 6
    cls.add();
}

Output of the code is:
Constructor
12
Destructor

In the above code section 1 creating a class name _class_ section 2 is the constructor of the class section 3 is a simple function that printing the two variable sum in section 4 we call the destructor to destroy all the class object and clear the memory from this class objects. This is the sample class with constructor and destructors.

Functions in classes

In the above example we created a function inside in class and call it in main function with a dot operator. We can create different function in a class and call it with dot operator like:

Example:

#include 
#include 
using namespace std;
// section 1
class _maths_{
    public:
    // section 2
        int adder(int x, int y){
            return x   y;
        }
    // section 3
        int factorial(int x){
            int result = 1;
            for(int i = 1; i < x;   i){
                result = x * i;
            }
            return result;
        }
    // section 4
        float circle(float x){
            return x * 3.14;     }};

int main(){
    // section 5
  _maths_ math;
    // section 6
  cout << math.factorial(21);
}
In above example i created a simple math class which have some function. In section 1 a class name _maths_ in section 2, 3, 4 have some simple math function adder return sum of two variable, factorial find the factorial of the a integer and circle is find the area of the circle. In main function we created a object and section 6 we call the different function to finding values. This is simple math class with some math functions. We can call this function in the all program where want to find the some values. In output i call every function of the class and see the result.
cout << math.factorial(45);
output is: 1980

cout << math.adder(5, 34);
output is: 39

cout << math.circle(5);
//output is: 15.7
Related Post's

cpp: function overloading   cpp: function   cpp: strings   cpp  
Not Loaded Zohaib Jozvi
0 1 June 2021
Read Latest Post.
Read other post.