Sample Programs Classes in C
Sample Programs Classes in C
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
Notice that the parameter of this constructor has the address of an object of the Wall class.
We then assign the values of the variables of the obj object to the corresponding variables of the
object calling the copy constructor. This is how the contents of the object are copied.
In main(), we then create two objects wall1 and wall2 and then copy the contents of wall1 to wall2:
Here, the wall2 object calls its copy constructor by passing the address of the wall1 object as its
Note: A constructor is primarily used to initialize objects. They are also used to run a default code
In C++ programming, we can pass objects to a function in a similar manner as passing regular
arguments.
#include <iostream>
using namespace std;
class Student {
public:
double marks;
// constructor to initialize marks
Student(double m) {
marks = m;
}
};
int main() {
Student student1(88.0), student2(56.0);
return 0;
}
Run Code
Output
Average Marks = 72
Here, we have passed two Student objects student1 and student2 as arguments to
the calculateAverage() function.
Example 2: C++ Return Object from a Function
#include <iostream>
using namespace std;
class Student {
public:
double marks1, marks2;
};
return student;
}
int main() {
Student student1;
// Call function
student1 = createStudent();
return 0;
}
Run Code
Output
Marks1 = 96.5
Marks2 = 75
Return object from
function in C++
In this program, we have created a function createStudent() that returns an object of Student class.
// Call function
student1 = createStudent();
Here, we are storing the object returned by the createStudent() method in the student1
In this tutorial, we will learn about operator overloading with the help of examples.
In C++, we can change the way operators work for user-defined types like objects and structures. This
Suppose we have created three objects c1, c2 and result from a class named Complex that represents
complex numbers.
Since operator overloading allows us to change how operators work, we can redefine how
the + operator works and use it to add the complex numbers of c1 and c2 by writing the following
code:
result = c1 + c2;
Note: We cannot use operator overloading for fundamental data types like int, float, char and so on.
To overload an operator, we use a special operator function. We define the function inside the class or
class className {
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};
Here,
• operator is a keyword.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
Output
Count: 6
Here, when we use ++count1;, the void operator ++ () is called. This increases the value attribute for
Note: When we overload operators, we can use it to work in any way we like. For example, we could
However, this makes our code confusing and difficult to understand. It's our job as a programmer to
The above example works only when ++ is used as a prefix. To make ++ work as a postfix we use this
syntax.
Notice the int inside the parentheses. It's the syntax used for using unary operators as postfix; it's not a
function parameter.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
Run Code
Output
Count: 6
Count: 7
The Example 2 works when ++ is used as both prefix and postfix. However, it doesn't work if we try
// Error
result = ++count1;
This is because the return type of our operator function is void. We can solve this problem by
Count operator ++ () {
// code
}
#include <iostream>
using namespace std;
class Count {
private:
int value;
public
:
// Constructor to initialize count to 5
Count() : value(5) {}
return temp;
}
return temp;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1, result;
return 0;
}
Run Code
Output
Count: 6
Count: 6
Here, we have used the following code for prefix operator overloading:
return temp;
}
The code for the postfix operator overloading is also similar. Notice that we have created an
temp.value = ++value;
The variable value belongs to the count1 object in main() because count1 is calling the function,
result = num + 9;
When we overload the binary operator for user-defined types by using the code:
The operator function is called using the obj1 object and obj2 is passed as an argument to the
function.
Example 4: C++ Binary Operator Overloading
#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;
}
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;
return 0;
}
Run Code
Output
However,
• using & makes our code efficient by referencing the complex2 object instead of making a
• using const is considered a good practice because it prevents the operator function from
modifying complex2.
Overloading binary
operators in C++
1. Two operators = and & are already overloaded by default in C++. For example, to copy
objects of the same class, we can directly use the = operator. We do not need to create an
operator function.
2. Operator overloading cannot change the precedence and associativity of operators. However,
a. :: (scope resolution)
b. . (member selection)
d. ?: (ternary operator)