Question

In c++ What does operator overloading allow us to do ? Why must the << and...

In c++

  • What does operator overloading allow us to do ?

  • Why must the << and the >> operators be overloaded as friend functions instead of member functions, for a user defined class ?

  • What is the difference between an instance member variable and a static member variable ?

  • How are call to static member functions made ?

  • Describe the difference between reading a file using the >> operator and with the getline function

  • What is inheritance

  • What is a base case ?

  • What does operator overloading allow us to do ?

  • What can a derived class access within a base class ?

  • An operator must be overloaded as a friend function when…

  • A protected member of a class can be accessed by

  • When can you overload an operator as a member function ? When must you overload an operator as a friend function ?

  • If you have class GradStudent : public Student

        What can the class GradStudent access within the class Student?

        What can a client of GradStudent access within the class Student?

  • Suppose you have a class called Employee that has, as a private member, an object of a class called Date.

       What can the class Employee access within the class Date ?

       What can a client of Employee access within the class Date?

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

Question 1: What does operator overloading allow us to do?

Answer: Operator overloading is a great feature of C++ using which we can define operators for objects of user-defined classes. For example, suppose we have a Complex class whose object represents a complex number of the form x + iy. If we want to add two objects of Complex class using plus(+) operator like c1 + c2 then we will not be able to do so without overloading a + operator for Complex class's objects.

Question 2: Why must the << and the >> operators be overloaded as friend functions instead of member functions, for a user-defined class?

Answer: Both << and >> are binary operators and the first operator of both of them is an object of C++'s istream class like cin, cout, etc. When operators are overloaded as member functions of the class then that means the first operator will always be the object of that class but if we do not want the first operator to be the object of that class then we need to declare it a friend function and define the first operator in the argument list of the operator function.

Question 3: What is the difference between an instance member variable and a static member variable?

Answer: An instance member variable is created whenever an object is created and the constructor is called and is associated with objects of the class i.e., for every object an instance member variable is also created. On the other hand, the static member variable is associated with a class i.e., only a single variable is created and shared among all objects of the class and also using the class's name. A static member variable is created using the "static" keyword.

Question 4: How are call to static member functions made?

Answer: A static member function is a special member function of a class which can only be used to access static member variables. A static member function can be called using an object of the class or class's name followed by scope-resolution operator (::) and function's name. For example, consider the class A:

class A{
public:
    static void print(){
        std::cout << "Inside A" << std::endl;
    }
}

The static member function print() can be called using:

A a; // Create an object of class A
a.print(); // Calling static function using object of class
A::print(); // Calling static function using class name

AS PER CHEGG'S ANSWERING GUIDELINES ONLY FIRST 4 QUESTIONS HAVE BEEN ANSWERED.

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
In c++ What does operator overloading allow us to do ? Why must the << and...
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
  • Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...

    c++ format Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Science Linux systems. If your code does not compile on CS machines you will get 0 for the assignment. Organize your files into folders by lab assignment. For this assignment, create a folder called Lab9. A. Read Chapter 18 B. Create a new class called RationalNumber to represent fractions. The class should have the following capabilities: 1) It should...

  • C++ questions T/F The compiler will report an error if the + operator is overloaded in...

    C++ questions T/F The compiler will report an error if the + operator is overloaded in a way so that it performs subtraction. T/F When a unary operator is overloaded using a member function, it accepts one explicit argument and returns a class object. T/F The size of an operator cannot be overloaded. T/F Operator overloading also allows creation of new operators. T/F To convert a class object to basic data type, a conversion operator needs to be included publicly...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • C++ (5 pts in total) Step 1: Operator Overload (3pts) Let's revisit your lab 5. Make...

    C++ (5 pts in total) Step 1: Operator Overload (3pts) Let's revisit your lab 5. Make an operator overload function for comparing shapes based on their area. For example, you want to have the ability to compare two shapes using the > and < operators, such as if ($1 > s2). In order to do this, you need to make an operator overload for > and < bool operator>(const Shape &, const Shape &); bool operator<(const Shape &, const Shape...

  • (C++ Program) 1. Write a program to allow user enter an array of structures, with each...

    (C++ Program) 1. Write a program to allow user enter an array of structures, with each structure containing the firstname, lastname and the written test score of a driver. - Your program should use a DYNAMIC array to make sure user has enough storage to enter the data. - Once all the data being entered, you need to design a function to sort the data in descending order based on the test score. - Another function should be designed to...

  • Part t True er Fahe ( pts each) Tue or false? true F or fase -C++8...

    Part t True er Fahe ( pts each) Tue or false? true F or fase -C++8 generally regarded as a low-level language. Given x S, y 3, the exprossion (x<y)&& ((y > 0) 11 (y-= x + 2)) istrue hh writing computer pograms, it's best practice to write all your functions and the entire pogram, befbre testing any one of the functions or program segments by itself Operators have inputs and return outputs. When an arthmetie operator has been overloaded...

  • Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a...

    Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....

  • Write a class called Book that has two private member variables called page (integer) and topic...

    Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say: book2 = book1 + 4; then...

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