In: Computer Science
What are the object oriented concepts and which all
object oriented concepts are used in the
given program? Consider the following code and explain how each of
the object oriented
concepts are applied in the given program. (CO1)
class Vehicle
{
string brand;
public:
void honk();
void honk(int);
};
void Vehicle::honk()
{
cout << "Tuut, tuut! \n" ;
}
void Vehicle::honk(int x)
{
for(int i=0;i<x;i++)
cout << "Tuut, tuut! \n" ;
}
int main()
{
Vehicle V1;
V1.honk();
V1.honk(3);
}
The object-oriented concepts available in the given program are as follows:
Please refer to the comments of the program for more clarity and understanding.
#include<bits/stdc++.h>
using namespace std;
/*
Below we are creating the Vehicle class
*/
class Vehicle {
string brand;
/*
Below is the condition of method overloading
Here, honk() method is overloaded
One method is with a integer parameter and other method is without any parameter
*/
public: void honk();
void honk(int);
};
void Vehicle::honk() {
// Implementation of the honk() method without parameter
cout << "Tuut, tuut! \n";
}
void Vehicle::honk(int x) {
// Implementation of honk() method with a integer parameter
// The below loop will run for x times(integer passed in the parameter)
for (int i = 0; i < x; i++)
cout << "Tuut, tuut! \n";
}
int main() {
Vehicle V1; // Creating instance of the Vehicle class.
// Here V1 is the object of V1
V1.honk(); // Calling the honk() method of Vehicle class
V1.honk(3); // Calling the honk(int) method of Vehicle class
}
Output of the above program:
Please let me know in the comments in case of any confusion. Also, please upvote if you like.