Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programing help: Consider the below code. Identify the potential problem(s)

ID: 3859758 • Letter: C

Question

C++ Programing help: Consider the below code. Identify the potential problem(s) if any in two methods:

• DoWork1()

• DoWork2()

#include <iostream>

#include <string>

using namespace std;

class Automobile

{

private:

string Make;

int Year;

string Model;

public:

Automobile()

{

Make = "";

Model = "";

Year = 0;

cout << "Automobile default constructor is called." << endl;

}

Automobile(string make, string model, int year) :

Make(make),

Model(model),

Year(year)

{

cout << "Automobile constructor is called." << endl;

}

Automobile(const Automobile & src)

{

Make = src.Make;

Model = src.Model;

Year = src.Year;

cout << "Automobile copy-constructor is called." << endl;

}

public:

~Automobile()

{

cout << "Automobile destructor is called." << endl;

}

public:

const Automobile & operator=(const Automobile & src)

{

Make = src.Make;

Model = src.Model;

Year = src.Year;

cout << "Automobile assignment operator (=) is called." << endl;

return *this;

}

public:

static Automobile * CreateAutomobiles(int count)

{

return new Automobile[count];

}

static Automobile * CreateAutomobiles()

{

Automobile autoMobiles[3];

return autoMobiles;

}

};

static void DoWork1()

{

Automobile * p1 = Automobile::CreateAutomobiles(3);

Automobile * p2 = Automobile::CreateAutomobiles();

}

static void DoWork2()

{

Automobile * pAutomobiles = Automobile::CreateAutomobiles(3);

delete pAutomobiles;

}

int main()

{

DoWork1();

DoWork2();

}

Explanation / Answer

Please see the output:

jdoodle.cpp: In static member function 'static Automobile* Automobile::CreateAutomobiles()':
jdoodle.cpp:70:20: warning: address of local variable 'autoMobiles' returned [-Wreturn-local-addr]
Automobile autoMobiles[3];
^
Automobile default constructor is called.
Automobile default constructor is called.
Automobile default constructor is called.
Automobile default constructor is called.
Automobile default constructor is called.
Automobile default constructor is called.
Automobile destructor is called.
Automobile destructor is called.
Automobile destructor is called.
Automobile default constructor is called.
Automobile default constructor is called.
Automobile default constructor is called.
Automobile destructor is called.
Segmentation fault (core dumped)

Rintime error will happen

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote