Can anyone explain this?
1.
Suppose the following function is in the application program
that uses the Coord class (Coord_app.cpp):
int print_x(int x)
{
return x;
}
And we want to be able to do the following in main:
int main()
{
Coord C1(5, 8);
cout << get_x(C1) << endl;
}
The idea is that the function get_x expects an integer to be passed
to it, but if a Coord object is passed to the function it will be
converted to an integer. In this case get_x is passed the Coord
object C1(5, 8), which is converted to the integer 5. So the output
of the program would be:
5
What prototype specifically would need to be included in Coord.h to
make this work?
A-) explicit Coord(int = 0, int = 0);
B-) Coord(int = 0, int = 0);
C-) explicit operator int();
D-) operator int(); correct answer
===================================
2.
In the 2D array project, which of the options below contains a
valid call to the overloaded () operator?
array_2D arr1;
A-) operator()(arr1, 5, 7);
B-) arr1.operator()(5, 7); correct
C-) operator()(arr1)(5)(7);
D-) arr1.operator()(5)(7);
=====================================================
3.
In an inheritance relationship:
A base class can have a pure virtual constructor.
A derived class can have a virtual constructor.
A-)
1.. True
2.. True
B-)
1.. False
2.. False correct answer
C-)
1.. True
2.. False
D-)
1.. False
2.. True
Please find the explanations below:
1)
Given code::
int main()
{
Coord C1(5, 8);
cout << get_x(C1) << endl;
}
Here as mentioned get_x expected parameter to be interger. SO if
trying to do get_x(C1) will cause run time error.
But it can be resolved by overloading int() operator for the class.
Once overloaded than calling class object will
return the value int.
Hence operator int(); required to be in class header
file.
2)
To overload parenthesis following is the code..
return type Class::operator()(int row, int col)
Hence arr1.operator()(5, 7); is correct
answer.
3)Virtual function works based on virtual table . Virtual table of
a class created when constructor of that class called. But if
constructor itself is virtual than there will be no any virtual
table where it can assign memory. Hence even base or drive
class
constructors cannot be virtual
Can anyone explain this? 1. Suppose the following function is in the application program that uses...
Your goal is to create an ‘Array’ class that is able to hold
multiple integer values. The ‘Array’ class will be given
functionality through the use of various overloaded operators
You will be given the main() function for your program and must add
code in order to achieve the desired result. Do not change any code
in main(). If something is not working, you must change your own
code, not the code in main().
Assignment 5: overloading member functions. Overview:...
QUESTION 18 According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type. 1. (a) True 2. (b) False QUESTION 19 According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in...
create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...
C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...
Transform the find function of Question 4 into a function template. Here is the program used to test your template, followed by the output of that program: #include <iostream> #include <string> #include "find.h" using namespace std; #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0])) int main() { cout << "int" << endl; cout << "---" << endl; int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cout << "3 is at location " << find(arr1, NUM_ELEMENTS(arr1),...
Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...
**Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of your own design. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called playthat should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var...
Please Write the following program in c# You are to write an application which will create a company, add agents (their name, ID, and weekly sales) to that company, and printout the details of all agents in the company. The application will contain three classes: Agent.cs, Company.cs, and CompanyTest.cs (this class is already provided). Flow of the application: The CompanyTest class creates an object of the Company class and reserves space for five agents. At this point if you call...
Using C++ to write .cpp and .h file. Main function and sample
output are given. The task is to write a vector class for dynamic
allocation.declare the the class named vector with the required
attributes. The task are defined in the main function.
#include <iostream> #include "vector.h". #define LOG(x,y) std::cout << x << y « std::endl; #define INFO(x) std::cout << "[INFO]: #define WARNING(x) std::cout <« "[WARNING]: "<< x <« std: :endl; << x << std::endl; " « x < std::endl;...
1.Suppose that the goop function from the previous question changes the value of z[1]. Does this change effect the value of the actual argument? A. Yes B. No 2.Here is a function declaration: void goo(int* x) { *x = 1; } Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you print *a after the function call goo(a)? A. 0 B. 1 C. address of a D. address...