C++ questions How can you inline a class method? Is the inline keyword required?
ID: 3885098 • Letter: C
Question
C++ questions
How can you inline a class method? Is the inline keyword required? Given that a class Complex exists properly declare an array to hold as many as 50 Complex objects. Assuming your program has already read in a certain number of Complex values, further assume that you'd like to take their average. It may help if you also knew that that this class has a method to add two Complex objects: Complex Complex:: add(const Complex & b) const: and a method to divide a Complex object by a floating-point value: Complex Complex:: divide(double n) const: Show your code to average all the elements in your array of values. (what is the data type of the average of two or more complex numbers?) Show the code for an addition method for a Complex class. (You of course recall that a complex number has both a real and an imaginary part.) (Reminder: Adding two values does not change either of the values, but creates a third value which is the original sum.) (Thought-provoker: Some programmers might find it useful if Complex addition were compatible with the built-in types - double, for instance.)Explanation / Answer
10.Defining class methods inside the class makes them inline. No inline keyword is required.
11. Complex array[50];
Complex sum;
Complex avg;
for (int i = 0 ; i<50; i++){
sum = sum.add(array[i]);
}
avg = sum/50;
12 Complex Complex::add(const Complex &b) const{
Complex sum;
sum.setReal(real + b.getReal());
sum.setimaginary(imaginary + b.getImaginary());
return sum;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.