erros of the program and correct them Answer: dentsponding lines if applicable E
ID: 3914556 • Letter: E
Question
erros of the program and correct them Answer: dentsponding lines if applicable Example: (2)should be:Winclude-iostream /K4 020,0; (10) (++ptr); onst iat" const cptr-arr /(12) 3%) Identify the errors of the program and correct fer the corresponding lines if applicable. Note: (1) Statioc member variable has to be defined outside of the class; (2) Satic member function cannot access non-static member dt (3) Static member function cannot access non-static member function. cass X ( Answer static int j 0 xiïnt ijs 0) : ?(ii) { j-i; } int val) const return i; static int incrO refurn+j static int fo) t return incrO: /(10) (13) l/(15) /(16) int main /(20)Explanation / Answer
6) Follwing are the errors in the given program:
Correct Porgram:
#include <iostream>
using namespace std;
int main(){
const int a = 10; // assign some value to a
const int c=0;
const int* ptr = &c;
*(ptr)++; // move * out of ()
const int arr[] = {10, 20, 30};
ptr = arr;
cout<<*(++ptr);
const int* const cptr = arr; // assign address of c
//cptr = &c;
}
7) Follwing are the errors in the given program:
Correct Program:
class X {
static int i; // make i static variable and change constructor (error: invalid use of member 'X::i')
static int j; // initialize it after class definition (error: ISO C++ forbids)
public:
X() { j = i; } // change constructor (error: invalid use of member 'X::i')
int val() const {return j;}
static int incr(){
i++;
return ++j;
}
int f(){ // make function non static (error: cannot call member function 'int X::val() const' without object)
val();
return incr();
}};
int X::j = 0; // initialization after class (erro: ISO C++ forbids)
int main() {
X x;
X* xp = &x;
x.f();
xp->f();
x.f();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.