Question
help with these c++ questions I believe I had submitted the wrong answer and want to know if I had got it right
[8 points) Consider the following code: 16 Foo :: Foo() { delete () arr; 2 17 } 4 class Food public: Foo(); *Foo(); private:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

problem:

class Foo{
public:
Foo();
~Foo();
private:
int *arr;
};
Foo::Foo(){
arr = new int[3];
for(int i=0;i<3;i++)
{
arr[i]=i;
}
}
Foo::~Foo(){
delete []arr;
}
void init(Foo& param)
{//the problem is in this method
Foo obj;//here we are creating a static local object of Foo, its life span is in this method only
param =obj;//now here we are assigning this local object to passed parameter param
//so for now,in this method param points the memory of the object obj
//but whenever this method terminates, the memory created for obj is also freed
//means after termination of this method, param points to meaning less address
//hence myobj.arr will start changing unpredictably
}
int main()
{
Foo myobj;
init(myobj);
return 0;   
}

solution:

class Foo{
public:
Foo();
~Foo();
private:
int *arr;
};
Foo::Foo(){
arr = new int[3];
for(int i=0;i<3;i++)
{
arr[i]=i;
}
}
Foo::~Foo(){
delete []arr;
}
void init(Foo& param)
{
Foo *obj = new Foo();//creating dynamic memory, it spans throughout the program
//assigning it to param
param = *obj;
  
  
}
int main()
{
Foo myobj;
init(myobj);
return 0;   
}

Add a comment
Know the answer?
Add Answer to:
help with these c++ questions I believe I had submitted the wrong answer and want to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I had a few questions in Computer Science (Java). I got these questions wrong and I...

    I had a few questions in Computer Science (Java). I got these questions wrong and I would like the CORRECT answer and an EXPLANATION PLEASE. Please use the correct number when giving the answer such as 1a:. 1b: etc 1. For Each of the following Bits of code enter the final value of the variable names ans. a. int x = 23; int y = 7; Int ans = x % y; My answer 1.61 (Wrong It was 2) b....

  • C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream>...

    C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream> template <typename T, int N=10> class Array { private:   T array[N+1]; public:   Array() {     for(int i=0; i<N+1; i++) array[i] = 0;   }      T& operator [] (int index) {     if (index>N || index < 0)       return array[N];     else       return array[index];   }   template <typename S>   Array<T,N>& operator= (S &other) {     for(int i=0; i<N+1; i++)       array[i] = other[i];         return *this;   } }; template <typename T, typename...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • Hello, There are some elements in my code that aren't working properly that need to be...

    Hello, There are some elements in my code that aren't working properly that need to be fixed but I don't know what I have done wrong. The printHealthBars() should print out a '+' for each point of current health and a '-' for each difference between current & max health. So, for instance, suppose the pokemon had 6/8 health, the method should print: "[ ++++++-- ]" Something is up with the damage calculation method. The idea is that it should...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE...

    HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE PROGRAM RUNS. Task 1: Enforcing const-ness throughout Your first job will be to go through all of the code and decide which functions should be declared const. You should find several places throughout the program where this makes sense. We will also make the id data member in the Customer class const , as once a customer has been created their ID will never...

  • I need help for part B and C 1) Create a new project in NetBeans called...

    I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...

  • please help answer 11-25 please i have already submitted this but only got 1-10 13 UH HN C) tertiary alcohol D) phe...

    please help answer 11-25 please i have already submitted this but only got 1-10 13 UH HN C) tertiary alcohol D) phenol E) thiol OH CONH2 --OH Tetracycline, an antibiotic Questions 6-10. Given the following list of acid strengths of some medications match the acid to the description. Name Acid pka 6. Strongest Acid A pentacillamine HCSH, NO,S 1.18 7. Weakest acid B. levadopa HC H. NO. 2.13 8. Diprotic acid C. citric acid H.C.H.O 3.08 9. Equal amounts of...

  • Please Answer the questions I got wrong(#5,#8,#10). Comprehenslve Accounting Cycle Revlew 11-02 (Part Level Submisslon) Karen...

    Please Answer the questions I got wrong(#5,#8,#10). Comprehenslve Accounting Cycle Revlew 11-02 (Part Level Submisslon) Karen Noonan opened Shamrock Cleaning Services Inc. on February 1, 2022. During February, the following transactions were completed: Issued 4,200 shares of Shamrock Cleaning Services common stock for $10,920. Each share has a $1.50 par Borrowed $6,720 on a 2-year, 6% note payable. Paid $7,580 to purchase used floor and window cleaning equipment from a company going out of business ($4,050 was for the floor...

  • Pleade,can you help me to answer these questions Please, Please , I need help 11:374 ..1...

    Pleade,can you help me to answer these questions Please, Please , I need help 11:374 ..1 LTED mail-attachment.googleusercontent.com When the them during the time of the paper e The of his loyal flowe A. The Arabe The Mega Whew is Princesa Te C ry To lead the Cle narpathare the நாளெ m e ன ப thaay AC Richading price is from of the Disewwing was wych So why Prish How did a lot of the Sampollet As the conditions and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT