Question

C++ class: Please answer the below question in paragraph format. There are several object-oriented languages that...

C++ class: Please answer the below question in paragraph format.

There are several object-oriented languages that do not support operator overloading. Discuss the role and the importance of operator loading for the design of a class.

Discuss what the “this” pointer provides to programming and the role it plays in operator overloading.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Operator Overloading is the process of defining the way in which the computations on objects is done when an operation using an operator is applied on them. e.g: Suppose there is a class Set which stores set of numbers. So, if there are two objects ob1 and ob2 and if we want to add them, how will we do it because the + operator is defined for data types like integer, double , float etc. But not for a user defined class. In order to solve this issue, we can overload the operator + and define what computations must be done when we do

ob1 + ob2

So, we need operator overloading to define the behaviour of the operator and how the operation will be done.

The structure of operator overloading function is

// replace + with other operators like * , / , - , == , < , > etc

return_type operator+( ClassName& ob2 )

{

    // computations to be done on the objects of class

}

this pointer in C++ is used to refer to the class variables or the methods. These are mainly used to differentiate between class variables and other local variables with the same name as the class variables.

e.g:

class Student

{

    string name;

    int age;

   

    public:

   

        // the arguments have the same name as class variables

        Student(string name , int age)

        {

            // this->name refer to the class variable

            this->name = name;

           

            // this->age refer to the class variable

            this->age = age;

        }

}

Add a comment
Know the answer?
Add Answer to:
C++ class: Please answer the below question in paragraph format. There are several object-oriented languages that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Directions: C++. Answer the question below in paragraph format. Many people think of programming as simply...

    Directions: C++. Answer the question below in paragraph format. Many people think of programming as simply writing the code. By now, you have seen that design of the program is an important part of programming. Design is one of the initial steps in creating a program. Testing is part of the final steps in programming. There are many different forms of testing. Compare and contrast the testing concepts of Unit Testing or Integration testing.

  • Object Oriented Programming Question 5 (a) Brieflfly explain the role of a constructor in C++? [3...

    Object Oriented Programming Question 5 (a) Brieflfly explain the role of a constructor in C++? [3 marks] (b) Can you create instance of a class without a constructor defifined in source code? Brieflfly explain your answer. [3 marks] (c) Objects model state and exhibit behaviour. Explain how state and behaviour are implemented in C++. [2 marks] [Total for Question 5: 8 marks]

  •    i am taking a c++ class. please answer these four question detailedly. thank you in...

       i am taking a c++ class. please answer these four question detailedly. thank you in advance. Know how to do the tracing problems with classes, aka BaskinRobbins type problem What are some of the benefits to Object Oriented programming? What do the concepts of object reusability and data hiding mean? How do you access a data member of a structure/class? How do you pass an array to a function?

  • In c++ redefine the class personType to take advantage of the new features of object-oriented design...

    In c++ redefine the class personType to take advantage of the new features of object-oriented design that you have learned, such as operator overloading, and then derive the class customerType. personType: #include <string> using namespace std; class personType { public: void print() const; //Function to output the first name and last name //in the form firstName lastName.    void setName(string first, string last); //Function to set firstName and lastName according to the //parameters. //Postcondition: firstName = first; lastName = last...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)*...

    PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)***** Also please dont use BigInteger thanks it's really appreciated for all the help PLEASE MAKE IT SO ITS USER INPUT Create a class called Rational for prforming arithmetie with fractions. Write an app to test your class Use integer variables to represent the private instance variables of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • C++ please Project: Working with Text Write an object-oriented program the performs the following tasks: ....

    C++ please Project: Working with Text Write an object-oriented program the performs the following tasks: . Reads a text file provided along with this data and maintains it an object. Determines the number of characters and keeps in the object. Determines the number of words and retains the result in the object. Determines the number of paragraphs and keeps the result in the object. A possible class definition: class Textutil { string text = ** int words = @; int...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • QUESTION 18 According to class materials, the program is allowed to overload operators only if at...

    QUESTION 18 According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type. 1. (a) True 2. (b) False QUESTION 19 According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT