Question

In: Computer Science

Research "Const Correctness" and answer the following questions: Given: class SimpleClass { private: int _x; public:...

Research "Const Correctness" and answer the following questions:

Given:

class SimpleClass
{
    private:
    int _x;

    public:
    SimpleClass(int x) : _x(x){}

    int getX() const
    {
        return _x;
    }

    void setX(int newX)
    {
        _x = newX;
    }

    void displayDataWithCustomMessage(const string &customMessage)
    {
        cout<<"Data: "<<_x<<endl;
        cout<<"Custom Message: "<<customMessage<<endl;
    }
};
  1. What is the usefulness of the "const" keyword in the definition of the "getX" member function?
  2. What is the usefulness of the "const" keyword in the definition of the "displayDataWithCustomMessage" member function?
  3. Why shouldn't the "const" keyword be used in the definition of the "setX" member function?

Solutions

Expert Solution

1. What is the usefulness of the "const" keyword in the definition of the "getX" member function?

The const keyword is used in the definition of getX() member function to allow it to be called by const or non const object from main method. If const keyword is not used , we cannot call this function getX() from const object.

2. What is the usefulness of the "const" keyword in the definition of the "displayDataWithCustomMessage" member function?

const SimpleClass t(10); // const object
   cout<<t.getX();
   t.displayDataWithCustomeMessage(" is verified"); // will generate compilation error.

3. Why shouldn't the "const" keyword be used in the definition of the "setX" member function?

Set methods are used to change the values of variables . But const methods do not allow to change the variable values. So const keyword should not be used with setX();

#include <iostream>
using namespace std;

class SimpleClass
{
private:
int _x;

public:
SimpleClass(int x) : _x(x){}

int getX() const // if we don't use const method , const objects cannnot call this function
{
return _x;
}

void setX(int newX)
{
_x = newX;
}

void displayDataWithCustomMessage(const string &customMessage)
{
cout<<"Data: "<<_x<<endl;
cout<<"Custom Message: "<<customMessage<<endl;
}
};

int main() {
  
   // both const and non const objects can call const functions
   const SimpleClass t(10); // const object
   cout<<t.getX();
   t.displayDataWithCustomeMessage(" is verified"); // will generate compilation error.
  
  
   SimpleClass t1(10); // non const object
   cout<<t1.getX();
   t1.displayDataWithCustomeMessage(" is verified");
  
   return 0;
}

Do ask if any doubt. Please upvote.


Related Solutions

#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
JAVA -The next three questions use the following class: class Identification { private int idNum;   ...
JAVA -The next three questions use the following class: class Identification { private int idNum;    public Identification() { this(0); }    public Identification(int startingIdNum) { idNum = startingIdNum; }    public int getIdNum() { return idNum; }    public void setIdNum(int idNum) { this.idNum = idNum; } } Here is one program using the above class: public class Main {    public static void main(String[] args) {        Identification i1 = new Identification();        Identification i2 =...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT