Question

In: Computer Science

Consider this class with a const variable and function: class Student { private: const int IDNum;...

Consider this class with a const variable and function:
class Student {
private:
const int IDNum;
public:
Student();
int getIDNum() const:
};
a. How do you initialize the const variable IDNum to 100? Write the code:
b. Are getters normally declared const?
c. Are setters normally declared const?
d. Can a const function call a non-const function?
e. Can a non-const function call a const function?
f. Can a const function use a non-const variable?
g. Can a non-const function use a const variable?

Solutions

Expert Solution

a. How do you initialize the const variable IDNum to 100? Write the code:
We have to use initializer list as follows to initialize IDNum to 100 in the constructor

Student::Student():IDNum(100){
}


b. Are getters normally declared const?
Yes, getters are normally declared as const, since we simple return the values of member variables without manipulating them or updating them

c. Are setters normally declared const?
No, we cannot declare setters as const, because in setters we modify/change
the value of member variables and doing so will give a compilation error


d. Can a const function call a non-const function?
No, const function can only call const functions


e. Can a non-const function call a const function?
Yes, non-const function can call const function

f. Can a const function use a non-const variable?
Yes, we can use non-const variables

g. Can a non-const function use a const variable?
Yes, non-const function can use const variable


Related Solutions

Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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 =...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
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; } }; What is the usefulness of the "const" keyword in the definition of the "getX" member function? What is the usefulness of the "const" keyword in the definition of the "displayDataWithCustomMessage" member function? Why...
#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 Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT =...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT = 5; const int BCOUNT = 7; const int CCOUNT = 6; int a[ACOUNT] = {1, 2, 3, 4, 5}; double b[BCOUNT] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[CCOUNT] = "HELLO"; cout <<"Array A contents: \n"; arrayContents(a, ACOUNT); cout <<"Array B contents: \n"; arrayContents(b, BCOUNT); cout <<"Array C contents: \n"; arrayContents(c, CCOUNT); return 0; } template void arrayContents(const T *arr, int countSize)...
use a match almost once. consider rhe following instance variable and method. private int[]arr; puplic int...
use a match almost once. consider rhe following instance variable and method. private int[]arr; puplic int i(intx){    \\PRECOND    for (intk =arr.length-1;k>=0;k--){    \\LOOPINVAR    is(arr[k]<x)returnk;    }    \\POSTCOND    RETURN-1; } Let:int m=f(n)                                        1.assert(a!=null);                                        2.assert(a.length>0);                                        3.assert(true)                                        4.assert(false);...
// function definitions go into hw08.cpp: // hw08.cpp namespace hw08 { int main(); const int ARRAY_SIZE...
// function definitions go into hw08.cpp: // hw08.cpp namespace hw08 { int main(); const int ARRAY_SIZE = 5; const int DYNAMIC_SIZE = 15; const int TIC_TAC_TOE_SIZE = 3; // function definitions: //------------------------------------------------------------------------------ int increment_value(int x) // pass a value, compute a new value by adding 5 to x and return it { // ... x += 5; // temp, replace when defining function return x;           // included so that incomplete lab code will compile } void increment_pointer(int* p)...
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code...
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code to throw a new IllegalStateException if the average is less than 0 And an IndexOutOfBoundsException if the average is > 0 28) Consider a student represented by the variable s1 and we are trying to set the average of s1. Write a try catch routine to catch either error listed above try { s1.setSemesterGrade(-30); s1.setSemesterGrade(200);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT