Question

In: Computer Science

**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...

**** IN C++ *****

1.Given the class alpha and the main function, modify the class alpha so the main function is working properly.

#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class alpha
{
private:
int data;
public:
//YOUR CODE

};
////////////////////////////////////////////////////////////////
int main()
{
alpha a1(37);
alpha a2;

a2 = a1;
cout << "\na2=";
a2.display(); //display a2

alpha a3(a1); //invoke copy constructor
cout << "\na3=";
a3.display(); //display a3
alpha a4 = a1;
cout << "\na4=";
a4.display();
cout << endl;
return 0;
}

Solutions

Expert Solution

#include <iostream>
using namespace std;
class alpha
{
private:
int data;
public:

//this is for alpha a2; this object creation if we remove it will raise an error.It is default constructor.
alpha(){;}

//this if for alpha a1(37); and it is a parameterized constructor with one parameter as integer type.
alpha(int d){

//assigning value to object.
   data=d;
}

//copy constructor. And this is for alpha a3(a1); and alpha a4=a1; these two lines.
alpha(const alpha &a){
   data=a.data;
}

//display function to display value of objects.
void display(){

//displaying data value of particular object invoked from main()
   cout<<data;
}
};
int main()
{
alpha a1(37); //invoking parameterized constructor
alpha a2;  //invoke default constructor
a2=a1; //invoke copy constructor
cout << "\na2=";
a2.display(); //display a2
alpha a3(a1); //invoke copy constructor
cout << "\na3=";
a3.display(); //display a3
alpha a4 = a1; //invoke copy constructor
cout << "\na4=";
a4.display(); //display a4
cout << endl;
return 0;
}

output:-


a2=37
a3=37
a4=37


Related Solutions

**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
This is for C++ A static main() function defined in your homework assignments means: 1.The function...
This is for C++ A static main() function defined in your homework assignments means: 1.The function is always invisible to all functions outside of the class. 2.The function cannot declare any variables. 3.The function can be called without an instance. 4.The function can access all variables and methods. 5.The function can be called by the superclass.
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT