4. Class and Objects

Class
A class in C++ is the building block, that leads to Object-Oriented programming. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object. 
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.
Syntax:-
class Box {
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};                         

 Objects
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.
Following statements declare two objects of class Box −

i). Box Box1;          // Declare Box1 of type Box
ii). Box Box2;          // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.

Accessing the Data Members
The public data members of objects of a class can be accessed using the direct member access operator (.).

Example:-
#include <iostream>
using namespace std;

class Box {
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};

int main() {
   Box Box1;        // Declare Box1 of type Box
   double volume = 0.0;     // Store the volume of a box here
 
   // box 1 specification
   Box1.height = 5.0; 
   Box1.length = 5.0; 
   Box1.breadth = 5.0;


   // volume of box 1
   volume = Box1.height * Box1.length * Box1.breadth;
   cout << "Volume of Box1 : " << volume <<endl;
   return 0;
}

Output:-
Volume of Box1 :125

It is important to note that private and protected members can not be accessed directly using direct member access operator (.). We will learn how private and protected members can be accessed.

Comparison of Class and Structure:-
Array with in Class:-
Arrays can be declared as the members of a class. The arrays can be declared as private, public or protected members of the class.

Example:-
#include<iostream> 
#include<array>     // for array, at() 
#include<tuple>    // for get() 
using namespace std; 
int main() 
    // Initializing the array elements 
    array<int,6> ar = {1, 2, 3, 4, 5, 6}; 
  
    // Printing array elements using at() 
    cout << "The array elements are (using at()) : "; 
    for ( int i=0; i<6; i++) 
    cout << ar.at(i) << " "; 
    cout << endl; 
  
    // Printing array elements using get() 
    cout << "The array elements are (using get()) : "; 
    cout << get<0>(ar) << " " << get<1>(ar) << " "; 
    cout << get<2>(ar) << " " << get<3>(ar) << " "; 
    cout << get<4>(ar) << " " << get<5>(ar) << " "; 
    cout << endl; 
  
    // Printing array elements using operator[] 
    cout << "The array elements are (using operator[]) : "; 
    for ( int i=0; i<6; i++) 
    cout << ar[i] << " "; 
    cout << endl; 
    return 0; 
  

Output:-

The array elemets are (using at()) : 1 2 3 4 5 6 
The array elemets are (using get()) : 1 2 3 4 5 6 
The array elements are (using operator[]) : 1 2 3 4 5 6 

 Passing an Object as argument
To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables.

Syntax:-

function_name(object_name);

Example:-  
#include <iostream.h> 
using namespace std; 
  
class Example { 
public: 
    int a; 
    // an object as an argument 
    void add(Example E) 
    { 
        a = a + E.a; 
    } 
}; 
  
int main() 
    Example E1, E2;   // Create objects 
  
    // Values are initialized for both objects 
    E1.a = 50; 
    E2.a = 100; 
  
    cout << "Initial Values \n"; 
    cout << "Value of object 1: " << E1.a 
         << "\n& object 2: " << E2.a 
         << "\n\n"; 
  
    // Passing object as an argument 
    // to function add() 
    E2.add(E1); 
  
    // Changed values after passing 
    // object as argument 
    cout << "New values \n"; 
    cout << "Value of object 1: " << E1.a 
         << "\n& object 2: " << E2.a 
         << "\n\n"; 
  
    return 0; 

Output:-
Initial Values 
Value of object 2: 50
& object 2:100

New values 
Value of object 2: 50
& object 2: 150

Post a Comment

0 Comments