bartleby

Concept explainers

Question
Book Icon
Chapter 11.6, Problem 11.26CP
Program Plan Intro

Operator overloading:

  • In C++ programming, operator overloading defines an operators as user defined classes and it performs the operations with one or more variables together.
  • When an overloaded operator is defined as the user-defined type for executing the operations, it should be defined as a member function. So, it will help to invoke the operands.
  • For manipulating data of the primitive data types, the C++ gives many operators.

Overloaded “[]” operator:

Overloaded operator contains many operator, the “[]” operator is the one of the type.

  • The Overloaded “[]” operator is used to access the array value.
  • It gives the ability to write a class that has an array like format.

Example:

Consider the example of overloaded “[]” operator is as follows:

// Include the header files

#include <iostream>

using namespace std;

// constant array size

const int S = 5;

// Declaration of the "Sample" class

class Sample

{

//access specifier

private:

//declare the variable

int a[S];

//access specifier

public:

//condtructor

Sample()

{

//declare the variable

int i;

//check the condition

for(i = 0; i < S; i++)

{

//set the value

a[i] = i;

}

}

//definition of overloaded "[]" operator

int &operator[](int i)

{

//check the condition

if( i >= S )

{

//display the error message

cout << "Index out of bounds" <<endl;

// return first element.

return a[0];

}

//otherwise

else

{

// return the element.

return a[i];

}

}

};

//main method

int main()

{

//create the object for the "Sample" class

Sample x;

//display the output

cout << "x[2] value is : " << x.operator[](2) <<endl;

cout << "x[5] value is : " << x.operator[](5)<<endl;

//return statement

return 0;

}

Blurred answer
Students have asked these similar questions
In Kotlin, Use this revised Employee class: data class Employee(val id: Int, val name: String, val wage: Double, val hours: Double) Create a list of Employees Write a function fireEmployee, which takes a list of Employees and an id number and returns a list that consists of all the original employees wihtout any that have that id. a) use filter in the function b) revise the function to take a function or a lambda to determine who gets fired. For example, you should be able to write a lambda and pass it to the function to result in everyone named "Xavier" getting fired. c) revise the function to use a tail-recursive helper function instead of filter
C++ Q3. Write a program which has a class template for determining the largest and the smallest number from a list of numbers. Use a constructor for input and appropriate data members and member functions in support of your answer
In C++ Call your class quadraticExpression.• Your private data will be a, b, c. Make them doubles.• Create an enumerated list witho NO_ROOTS = 0o ONE_ROOT = 1o TWO_ROOTS = 2o INFINITE_ROOTS = 3• Write the constructor as stated in the book• Write the changeCoeeficients as stated in the book• Write the getACoeeficient, getBCoeeficient,getCCoeeficient as inline constant functions• Write evaluate function to take a double a return a double. Constantfunction.• Write getNumberOfRoots function to return an integer. Constantfunction.• Write getFirstRoot and getSecondRoot to return the first and secondroots. Return a domain_error is there is no first or second root.Constant Functions.Use my .cpp file to test out your class

Chapter 11 Solutions

Starting Out With C++: Early Objects, Student Value Edition & Myprogramminglab With Pearson Etext -- Standalone Access Card Package, 9/e

Ch. 11.5 - When is a copy constructor called?Ch. 11.5 - How does the compiler know that a member function...Ch. 11.5 - What action is performed by a classs default copy...Ch. 11.6 - Assume there is a class named Pet. Write the...Ch. 11.6 - Assume that dog and cat are instances of the Pet...Ch. 11.6 - What is the disadvantage of an overloaded ...Ch. 11.6 - Prob. 11.17CPCh. 11.6 - Prob. 11.18CPCh. 11.6 - Assume there is a class named Animal, which...Ch. 11.6 - Prob. 11.20CPCh. 11.6 - Describe the values that should he returned from...Ch. 11.6 - Prob. 11.22CPCh. 11.6 - What type of object should an overloaded operator...Ch. 11.6 - Prob. 11.24CPCh. 11.6 - If an overloaded or operator accesses a private...Ch. 11.6 - Prob. 11.26CPCh. 11.6 - When overloading a binary operator such as or as...Ch. 11.6 - Explain why overloaded prefix and postfix and ...Ch. 11.6 - Prob. 11.29CPCh. 11.6 - Overload the function call operator ( ) (int i,...Ch. 11.8 - Prob. 11.31CPCh. 11.8 - How is the type declaration of an r value...Ch. 11.8 - Prob. 11.33CPCh. 11.8 - Prob. 11.34CPCh. 11.8 - Which operator must be overloaded in a class...Ch. 11.8 - Prob. 11.36CPCh. 11.10 - What arc the benefits of having operator functions...Ch. 11.10 - Prob. 11.38CPCh. 11.10 - Assume that there is a class named BlackBox. Write...Ch. 11.10 - Assume there are two classes, Big and Smal1.Write...Ch. 11.13 - What type of relationship between classes is...Ch. 11.13 - Why does it make sense to think of a base class as...Ch. 11.13 - What is a base class access specification?Ch. 11.13 - Think of an example of two classes where one class...Ch. 11.13 - What is the difference between private members and...Ch. 11.13 - What is the difference between member access...Ch. 11.13 - Suppose a program has the following class...Ch. 11.14 - What is the reason that base class constructors...Ch. 11.14 - Why do you think the arguments to a base class...Ch. 11.14 - Passing arguments to base classes constructors...Ch. 11.14 - What will the following program display? #include...Ch. 11.14 - What will the following program display? #include...Ch. 11 - If a member variable is declared _____, all...Ch. 11 - Static member variables are defined _____ the...Ch. 11 - A(n) _____ member function cannot access any...Ch. 11 - A static member function may be called _____ any...Ch. 11 - A(n) _____ function is not a member of a class,...Ch. 11 - A(n) _____ tells the compiler that a specific...Ch. 11 - _____ is the default behavior when an object is...Ch. 11 - A(n) _____ is a special constructor, called...Ch. 11 - _____ is a special built-in pointer that is...Ch. 11 - An operator may be _____ to work with a specific...Ch. 11 - When the _____ operator is overloaded, its...Ch. 11 - Making an instance of one class a member of...Ch. 11 - Object composition is useful for creating a(n)...Ch. 11 - A constructor that takes a single parameter of a...Ch. 11 - The class Stuff has both a copy constructor and an...Ch. 11 - Explain the programming steps necessary to make a...Ch. 11 - Explain the programming steps necessary to make a...Ch. 11 - Consider the following class declaration: class...Ch. 11 - Describe the difference between making a class a...Ch. 11 - What is the purpose of a forward declaration of a...Ch. 11 - Explain why memberwise assignment can cause...Ch. 11 - Explain why a classs copy constructor is called...Ch. 11 - Explain why the parameter of a copy constructor...Ch. 11 - Assume a class named Bird exists. Write the header...Ch. 11 - Assume a class named Dollars exists. Write the...Ch. 11 - Assume a class named Yen exists. Write the header...Ch. 11 - Assume a class named Length exists. Write the...Ch. 11 - Assume a class named Collection exists. Write the...Ch. 11 - Explain why a programmer would want to overload...Ch. 11 - Each of the following class declarations has...Ch. 11 - A derived class inherits the _____ of its base...Ch. 11 - The base class named in the following line of code...Ch. 11 - The derived class named in the following line of...Ch. 11 - In the following line of code, the class access...Ch. 11 - In the following line of code, the class access...Ch. 11 - Protected members of a base class are like _____...Ch. 11 - Complete the following table by filling in...Ch. 11 - Complete the following table by filling in...Ch. 11 - Complete the following table by filling in...Ch. 11 - When both a base class and a derived class have...Ch. 11 - When both a base class and a derived class have...Ch. 11 - An overridden base class function may be called by...Ch. 11 - Each of the following class declarations and/or...Ch. 11 - Soft Skills 44. Your companys software is a market...Ch. 11 - Check Writing Design a class Numbers that can be...Ch. 11 - Day of the Year Assuming that a year has 365 days,...Ch. 11 - Day of the Year Modification Modify the DayOfYear...Ch. 11 - Number of Days Worked Design a class called...Ch. 11 - Palindrome Testing A palindrome is a string that...Ch. 11 - Prob. 6PCCh. 11 - Corporate Sales A corporation has six divisions,...Ch. 11 - Prob. 8PCCh. 11 - Rational Arithmetic II Modify the class Rational...Ch. 11 - HTML Table of Names and Scores Write a class whose...Ch. 11 - Prob. 11PC
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education