C++ Objects

C++ class is a template for objects. The objects are created, destroyed and manipulated. The C++ objects make use of properties and methods of a class.

Advertisements

Declaring objects

There are two methods for creating objects.

  1. Declare objects with class declaration.
  2. Declare objects separately.

We will discuss both the method now.

Declare objects with Class Declaration

In this method, first you declare the class and then the object immediately.

For example

class student {
    char First_name [12];
    char Last_name [12];
    int Rollno;
    int total_marks;
    char grade;
public:
void compute_grade ()
{
    if (total_marks > 80)
    {
        grade = “A”;
    } 
    else if (total_marks >= 70 && total_marks < 80)
    {
        grade = “B”;
    } 
    else if (total_marks >= 50 && total_marks < 70)
    {
        grade = “C”;
    }
    else 
    {
        grade = “F”;
    }
    cout << “Your Grade is” << “ “ << grade << endl;
} student1, student2, student3; //declaring student object

Declaring Objects Separately

In this method, the object and the class declaration is done separately. The advantage of declaring this way is that you can initialize the object at the time of declaration itself.

Advertisements
class student { 
    char First_name [12];
    char Last_name [12];
    int Rollno;
    int total_marks;
    char grade;
public:
    void compute_grade () 
    { 
        if(total_marks > 80) 
        {
            grade = “A”; 
        } 
        else if (total_marks >= 70 && total_marks < 80) 
        { 
            grade = “B”; 
        } 
        else if (total_marks >= 50 && total_marks < 70) 
        {
            grade = “C”;
        } 
        else 
        {
            grade = “F”;
        }
        cout << “Your Grade is” << “ “ << grade << endl;
     }
}; student S4, S5; // global declaration of objects 
int main () 
{
    student S1, S2, S3; // declaration inside main ()
    return 0;
}

Accessing Class Members

You can access class members in just like structure or union. The main method of accessing the class member is using Dot (.) operator.

Syntax

The syntax for accessing a class member is given below.

<object_name>.data_member;
<object_name>.member_function ();

Example

S1.Rollno = 342556; 
S1.total_marks = 67; 
S1.compute_grade (); 
getch (); 
return 0;

Example Program Code:

The example program given below will create student objects and then get total_marks of a student object. Then compute the grade and display it immediately.

#include <stdlib.h> 
#include <iostream> 
#include <conio.h> 
#include <string> 
class student {
 
public:
    char First_name [12]; 
    char Last_name [12]; 
    int Rollno; 
    int total_marks; 
    char grade;
    char compute_grade () { 
        if (total_marks > 80) 
        {
            grade = 'A'; 
        } 
        else if (total_marks >= 70 && total_marks < 80)
        { 
            grade = 'B'; 
         } 
        else if (total_marks >= 50 && total_marks < 70) 
        { 
            grade = 'C'; 
        } 
        else 
        {
            grade = 'F';
        }
        return grade;
     } 
}; 
using namespace std;
int main ()
{
    char result; 
    student S1, S2; 
    strcpy(S2.First_name,"Gary"); 
    strcpy(S2.Last_name,"Button"); 
    S2.Rollno = 2464545; 
    S2.total_marks = 87; 
    result = S2.compute_grade(); 
    cout << S2.First_name << " " << S2.Last_name << " " << "You Grade is" << " " << result; 
    getch (); 
    return 0; 
}

Output – Example Program

The output of the above program is given below.

Output - C++ Objects
Output – C++ Objects

References

  • Balagurusamy. 2008. Object Oriented Programming With C++. Tata McGraw-Hill Education.
  • Maureau, Alex. 21-Jul-2013. Reviewing C++. Alex Maureau.
  • Ravichandran. 2011. Programming with C++. Tata McGraw-Hill Education.


Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.