Concept explainers
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;
}
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Starting Out with C++: Early Objects (9th Edition)
- What must you be sure of when passing a class object to a function template that uses an operator, such as * or >?arrow_forwardPPM (Portable Pixmap) use three integers to represent a pixel – this means we can have images with RGB colors. You will create a Pixel class in C++ which has three attributes: red: int green: int blue: int You will create a default constructor that initializes those values to 255, and an overloaded constructor that takes user input to assign the values. The class will also have the following functions: changeRGB (): Takes in three integers to update the red, green, and blue attributes. Returns nothing. printRGB (): Takes in nothing. Prints the red, green, and blue attributes in order with a single space in-between each value. Returns nothing. You will then recreate the art program from Assignment 5 with the following changes: Instead of a 2D array of integers, you will create a 2D array of Pixel object. Don’t be scared! This is similar to creating a 2D array of strings. You will prompt for three color values instead of one – red, green, and blue. These must be stored in a Pixel…arrow_forwardC++ OOParrow_forward
- C++ You may work on a single class. The parameter and return type s of each function and class member function should be decided in advance. The program will be best implemented as a multi-file program, (header file, the main program,..) Design a generic class to hold the following information about a bank account: Balance Number of deposits this month Number of withdrawals Annual Interest Rate Monthly service charges The class should have the following member function: Constructors Accepts arguments for the balance and annual interest rate. deposit a virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should also increment the variable holding the number of deposits. withdraw a virtual function that accepts an argument for the amount of the withdrawal. The function should subtract the argument from the account balance. It should also increment the variable…arrow_forwardIn c++ The student class defines the strings studName and studSSN as protected data members. Aconstructor has arguments that initialize the data. The class studentAthlete has a private datamember sport, which describes the sport the student plays. Both classes have a memberfunction identify(). The base class student outputs the student information in the form student studName Social Security Number studSSN The function in the base class should support polymorphism, and the function in the derivedstudentAthlete class should add the information “Sport <sport>”. In the file “student.h”,declareand implement the two classes in an inheritance hierarchy. In a program, initialize an array of five student pointers as follows: student ja(“John Anderson”, “345-12-3547”);student bw(“Bill Williams”, “286-72-6194”);studentAthlete bj(“Bob Johnson”, “294-87-6295”, “football”);studentAthlete dr(“Dick Robinson”, “669-28-9296”, “baseball”); // list of student pointersstudent *stud[] = {&ja,…arrow_forwardGiven the code:class DigitalWallet {private; long *id; // wallet id long *accounts; // dynamic array of account numbers int numAccounts; // number of user accounts in the array}1. Write a function definition for a Constructor to initialize all data members.2. Write a function definition for the Copy Constructor.3. Write a function definition for the Destructor.arrow_forward
- 1 def all_fluffy(s: str) -> bool: 2 """Return True iff every letter in s is fluffy. Fluffy letters are those 3. that appear in the word 'fluffy'. 4 #TODO: Add at least one example to the docstring and complete the body 7 8 9 10 #TODO: Now complete the function bodyarrow_forwardC++: OOP---Please comment the OOP concept. Thank you!arrow_forwardC++ help. In the class definition, initialize the data members, string type and integer age, with the default values "Unstated" and 0, respectively. Ex: If the input is mouse 13, then the output is: Type: Unstated, Age: 0 Type: mouse, Age: 13 Note: The class's print function is called first after the default constructor, then again after the inputs are passed to the setters.''' #include <iostream>#include <string>using namespace std; class Animal { public: void SetType(string animalType); void SetAge(int animalAge); void Print(); private: /* Your code goes here */ }; void Animal::SetType(string animalType) { type = animalType;} void Animal::SetAge(int animalAge) { age = animalAge;} void Animal::Print() { cout << "Type: " << type << ", Age: " << age << endl;} int main() { string newType; int newAge; Animal myAnimal; myAnimal.Print(); cin >> newType; cin >> newAge;…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education