Question: C++ Programming Help on 10 questions Q2: What will be the output of this program after an instance of Class5 is created? #include using namespace
C++ Programming Help on 10 questions
Q2: What will be the output of this program after an instance of Class5 is created?
#include
using namespace std;
class Class1
{
public:
Class1()
{
cout << "Class1 constructor is executed." << endl;
}
};
class Class2
{
public:
Class2()
{
cout << "Class2 constructor is executed." << endl;
}
};
class Class3
{
public:
Class3()
{
cout << "Class3 constructor is executed." << endl;
}
private:
Class1 c1;
Class2 c2;
};
class Class4
{
public:
Class4()
{
cout << "Class4 constructor is executed." << endl;
}
private:
Class3 c3;
};
class Class5 : public Class4
{
public:
Class5() : Class4()
{
cout << "Class5 constructor is executed." << endl;
}
};
Q3: What will be the output of this program after an instance of Class5 is created?
#include
using namespace std;
class Class1
{
public:
Class1()
{
cout << "Class1 constructor is executed." << endl;
}
};
class Class2
{
public:
Class2()
{
cout << "Class2 constructor is executed." << endl;
}
};
class Class3
{
public:
Class3()
{
cout << "Class3 constructor is executed." << endl;
}
};
class Class4 : public Class2, Class1
{
public:
Class4()
{
cout << "Class4 constructor is executed." << endl;
}
};
class Class5 : public Class4, Class3
{
public:
Class5()
{
cout << "Class5 constructor is executed." << endl;
}
};
Q4: What will be the output of this program after an instance of Class5 is created and then destroyed?
#include
using namespace std;
class Class1
{
public:
~Class1()
{
cout << "Class1 constructor is destroyed." << endl;
}
};
class Class2
{
public:
~Class2()
{
cout << "Class2 constructor is destroyed." << endl;
}
};
class Class3
{
public:
~Class3()
{
cout << "Class3 constructor is destroyed." << endl;
}
};
class Class4 : public Class2, Class1
{
public:
~Class4()
{
cout << "Class4 constructor is destroyed." << endl;
}
};
class Class5 : public Class4, Class3
{
public:
~Class5()
{
cout << "Class5 constructor is destroyed." << endl;
}
};
Q6: Implement a C++ functor (Multiply) that allows multiplying any value with every element in the array below.
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
int multiplyValue = 5;
transform(arr, arr + n, arr, Multiply(multiplyValue));
for (int i = 0; i cout << arr[i] << " "; return 0; } Expected output: 5 10 15 20 25 Q7: Consider the following code. Write sample code lines that demonstrate calling the copy constructor and the assignment operator of this class. #include #include using namespace std; class Automobile { private: string Make; int Year; string Model; public: 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; } }; Q8: Consider the below code. In this code, there are 4 classes (Automobile, Truck, Sedan, and CrossOverVehicle). The Automobile class is the based class of the Truck class and the Sedan class. The CrossOverVehicle inherits from both the Truck class and the Sedan class. In the main() method, the stack variable (crossOver1) is instantiated. Explain the reason why all three outputs (1), (2), and (3) are the same. #include #include using namespace std; // CLASS: Automobile class Automobile { private: string Make; int Year; string Model; public: string GetMake() { return Make; } int GetYear() { return Year; } string GetModel() { return Model; } void SetMake(string value) { Make = value; } void SetYear(int value) { Year = value; } void SetModel(string value) { Model = value; } 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; } }; // CLASS: Truck class Truck : virtual public Automobile { public: Truck() : Automobile() { cout << "Truck default constructor is called." << endl; } Truck(string make, string model, int year) : Automobile(make, model + " (Truck)", year) { cout << "Truck constructor is called." << endl; } Truck(const Truck & src) { cout << "Truck copy-constructor is called." << endl; *this = src; } public: ~Truck() { cout << "Truck destructor is called." << endl; } public: const Truck & operator=(const Truck & src) { Automobile::operator=(src); cout << "Truck assignment operator (=) is called." << endl; return *this; } }; // CLASS: Sedan class Sedan : virtual public Automobile { public: Sedan() : Automobile() { cout << "Sedan default constructor is called." << endl; } Sedan(string make, string model, int year) : Automobile(make, model + " (Sedan)", year) { cout << "Sedan constructor is called." << endl; } Sedan(const Sedan & src) { cout << "Sedan copy-constructor is called." << endl; *this = src; } public: ~Sedan() { cout << "Sedan destructor is called." << endl; } public: const Sedan & operator=(const Sedan & src) { Automobile::operator=(src); cout << "Sedan assignment operator (=) is called." << endl; return *this; } }; // CLASS: CrossOverVehicle class CrossOverVehicle : public Truck, public Sedan { public: CrossOverVehicle() : Truck(), Sedan() { cout << "CrossOverVehicle default constructor is called." << endl; } CrossOverVehicle(string make, string model, int year) : Automobile(make, model + " (CrossOverVehicle)", year) { cout << "CrossOverVehicle constructor is called." << endl; } CrossOverVehicle(const CrossOverVehicle & src) { cout << "CrossOverVehicle copy-constructor is called." << endl; *this = src; } public: ~CrossOverVehicle() { cout << "CrossOverVehicle destructor is called." << endl; } public: const CrossOverVehicle & operator=(const CrossOverVehicle & src) { Truck::operator=(src); Sedan::operator=(src); cout << "CrossOverVehicle assignment operator (=) is called." << endl; return *this; } }; int main() { CrossOverVehicle crossOver1("Mercedes", "X6", 2017); // OUTPUT #1 cout << endl; cout << "----------------------------" << endl; cout << "Truck Make: " << ((Truck *)&crossOver1)->GetMake() << endl; cout << "Truck Model: " << ((Truck *)&crossOver1)->GetModel() << endl; cout << "Truck Year: " << ((Truck *)&crossOver1)->GetYear() << endl << endl; // OUTPUT #2 cout << "----------------------------" << endl; cout << "Sedan Make: " << ((Sedan *)&crossOver1)->GetMake() << endl; cout << "Sedan Model: " << ((Sedan *)&crossOver1)->GetModel() << endl; cout << "Sedan Year: " << ((Sedan *)&crossOver1)->GetYear() << endl << endl; // OUTPUT #3 cout << "----------------------------" << endl; cout << "CrossOverVehicle Make: " << crossOver1.GetMake() << endl; cout << "CrossOverVehicle Model: " << crossOver1.GetModel() << endl; cout << "CrossOverVehicle Year: " << crossOver1.GetYear() << endl << endl; return 0; } Q9: The below code does not compile. Fix it and still keep the duplicate() method remain unedited #include using namespace std; class Rectangle { int width, height; public: Rectangle() {} Rectangle(int x, int y) : width(x), height(y) {} int area() { return width * height; } }; Rectangle duplicate(const Rectangle& param) { Rectangle res; res.width = param.width * 2; res.height = param.height * 2; return res; } int main() { Rectangle foo; Rectangle bar(2, 3); foo = duplicate(bar); cout << foo.area() << ' '; return 0; } Q10: Re-implement the Multiply functor class in so that it can be used with any basic type (such double, float, long, unsigned int, and etc). int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr) / sizeof(arr[0]); int multiplyValue = 5; transform(arr, arr + n, arr, Multiply(multiplyValue)); for (int i = 0; i cout << arr[i] << " "; return 0; } Expected output: 5 10 15 20 25 Q14: Implement code to sort the vector of numeric values below #include #include #include // Prints all elements in a vector template void PrintVector(vector { for (size_t i = 0; i < a.size(); i++) cout << a[i] << " "; cout << endl; } int main() { double values[] = { 32.0, 71.0, 12.0, 45.0, 26.0, 80.0, 53.0, 33.0 }; std::vector cout << "UNSORTED ARRAY:" << endl; PrintVector cout << endl; // TODO: Add code below this line to sort the values array of double(s) above. cout << "SORTED ARRAY:" << endl; PrintVector cout << endl; return 0; } Q15: Design and provide the Class Diagram for the Online Shopping system per requirements below. No C++ code would be required Just the Class Diagram only. Each customer has unique id and is linked to exactly one account. Account owns shopping cart and orders. Customer could register as a web user to be able to buy items online. Customer is not required to be a web user because purchases could also be made by phone or by ordering from catalogues. Web user has login name which also serves as unique id. Web user could be in several states - new, active, temporary blocked, or banned, and be linked to a shopping cart. Shopping cart belongs to account. Account owns customer orders. Customer may have no orders. Customer orders are sorted and unique. Each order could refer to several payments, possibly none. Every payment has unique id and is related to exactly one account. Each order has current order status. Both order and shopping cart have line items linked to a specific product. Each line item is related to exactly one product. A product could be associated to many line items or no item at all.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
