Examples of objects and classes in c++ Programming

0

A class is defined in C++ using keyword class followed by the name of the class.

Example :

class className

{

   // some data

   // some functions 

};

Objects :

When a class is defined, only the specification for the object is defined; no memory or storage is allocated.

Examples :

// sample function

void sampleFunction() {

    // create objects

    Room room1, room2;

}

int main()

{

    // create objects 

    Room room3,  room4;

}

All Contributions

Binary Operator Overloading

 program adds two complex numbers :

// C++ program to overload the binary 
#include <iostream>
using namespace std;
class Complex {
   private:
    float real;
    float imag;
   public:
    // Constructor to initialize real and imag to 0
    Complex() : real(0), imag(0) {}
    void input() {
        cout << "Enter real and imaginary parts respectively: ";
        cin >> real;
        cin >> imag;
    }
    // Overload the + operator
    Complex operator + (const Complex& obj) {
        Complex temp;
        temp.real = real + obj.real;
        temp.imag = imag + obj.imag;
        return temp;
    }
    void output() {
        if (imag < 0)
            cout << "Output Complex number: " << real << imag << "i";
        else
            cout << "Output Complex number: " << real << "+" << imag << "i";
    }
};
int main() 
{
    Complex complex1, complex2, result;
    cout << "Enter first complex number:\n";
    complex1.input();
    cout << "Enter second complex number:\n";
    complex2.input();
   // complex1 calls the operator function
   // complex2 is passed as an argument to the function
    result = complex1 + complex2;
    result.output();
    return 0;
}

Return Value from Operator Function (++ Operator):

#include <iostream>
using namespace std;
class Count {
   private:
    int value;
   public
       :
    // Constructor to initialize count to 5
    Count() : value(5) {}
    // Overload ++ when used as prefix
    Count operator ++ () {
        Count temp;
        // Here, value is the value attribute of the calling object
        temp.value = ++value;
        return temp;
    }
    // Overload ++ when used as postfix
    Count operator ++ (int) {
        Count temp;
        // Here, value is the value attribute of the calling object
        temp.value = value++;
        return temp;
    }
    void display() {
        cout << "Count: " << value << endl;
    }
};
int main() 
{
    Count count1, result;
    // Call the "Count operator ++ ()" function
    result = ++count1;
    result.display();
    // Call the "Count operator ++ (int)" function
    result = count1++;
    result.display();

    return 0;
}

Overload ++ when used as prefix and postfix :

#include <iostream>
using namespace std;
class Count {
   private:
    int value;
   public:
    // Constructor to initialize count to 5
    Count() : value(5) {}
    // Overload ++ when used as prefix
    void operator ++ () {
        ++value;
    }
    // Overload ++ when used as postfix
    void operator ++ (int) {
        value++;
    }
    void display() {
        cout << "Count: " << value << endl;
    }
};
int main() 
{
    Count count1;
    // Call the "void operator ++ (int)" function
    count1++;
    count1.display();
    // Call the "void operator ++ ()" function
    ++count1;
    count1.display();
    return 0;
}

++ Operator (Unary Operator) Overloading:

#include <iostream>
using namespace std;
class Count {
   private:
    int value;
   public:
    // Constructor to initialize count to 5
    Count() : value(5) {}
    // Overload ++ when used as prefix
    void operator ++ () {
        ++value;
    }
    void display() {
        cout << "Count: " << value << endl;
    }
};
int main() 
{
    Count count1;
    // Call the "void operator ++ ()" function
    ++count1;
    count1.display();
    return 0;
}

 program to calculate the average marks of two students 

#include <iostream>
using namespace std;
class Student {
   public:
    double marks;
    // constructor to initialize marks
    Student(double m) {
        marks = m;
    }
};
// function that has objects as parameters
void calculateAverage(Student s1, Student s2) {
    // calculate the average of marks of s1 and s2 
    double average = (s1.marks + s2.marks) / 2;
   cout << "Average Marks = " << average << endl;
}
int main() 
{
    Student student1(88.0), student2(56.0);
  // pass the objects as arguments
   calculateAverage(student1, student2);
    return 0;
}

total contributions (9)

New to examplegeek.com?

Join us