C++
class derivedFromTemp: public temp;
{public:
void print();
void setZ(double);
double getZ();
double power() const; //return X to power of Z assume
getX and setX in temp class.
drivedFromTemp();
derivedFromTemp (string, double, double);
Private:
double z;
};
A. Correct errors, if any, after that write difinition of the member function of the class derivedFromTemp.
Hi,
Please find below the answer-
1. Errors in the code-
a) Semi-colon is not required in the derived class. Remove the semi-colon and the code will work.
class derivedFromTemp: public temp;
b) The access modifier is defined as "Private" which is incorrect. Instead of that "private" keyword needs to be used.
Private: private:
double z; -- >>> double z;
c) Need to include library for pow function to calculate power. The library that needs to be included is math.h
#include <iostream>
#include <math.h>
d) Need to correct the spelling mistake while defining the non-parameterized constructor.
drivedFromTemp(); ---->> derivedFromTemp();
2. Correct code with function definitions-
#include <iostream>
#include <math.h>
using namespace std;
class temp {
public:
double x;
};
class derivedFromTemp: public temp
{public:
void print();
void setZ(double);
double getZ();
double power() const; //return X to power of Z assume getX and setX
in temp class.
derivedFromTemp();
derivedFromTemp (string , double, double );
private:
double z;
};
void derivedFromTemp::print(){
cout<<"x to the power of z is"<<power();
}
void derivedFromTemp::setZ(double m){
z=m;
}
double derivedFromTemp::getZ(){
return z;
}
double derivedFromTemp::power() const{
double val;
val=pow(x,z);
return val;
}
derivedFromTemp::derivedFromTemp (string s, double d1, double d2
){
x=d1;
z=d2;
}
int main()
{
cout << "Hello World" << endl;
return 0;
}
Screenshot-
Thanks and Regards,
Vinay Singh
C++ class derivedFromTemp: public temp; {public: void print(); void setZ(double); double getZ(); ...
17. Write a non-member function called centroid(param) that takes a static array of points and the size of the array and return the centroid of the array of points. If there is no center, return the origins private: double x, y, z public: /Constructors Point(); Point(double inX, double inY, double inZ = 0); Point(const Point& inPt); / Get Functions double getX() const; double getY) const; double getZ) const; Set Functions void setX(double inX); void setY(double inY); void setZ(double inZ); void...
class Circle { public: enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN, RED}; Circle(int = 0, int = 0, double = 0.0, Color = UNDEFINED); void setX(int); void setY(int); void setRadius(double); void setColor(Color); int getX() const; int getY() const; double getRadius() const; Color getColor() const; private: int x; int y; ...
Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...
What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }
please answer correctly public class Eve public void a print("Eve a ": public void bo print("Eveb "; public String toString() { return "Eve ts": public class Sam extends Eve public void bo a(): print("Sanb ": public String toString() { return "Sants": public class Lucas extends Sam public void a() { print("Lucas a ": print (toString() + " "); public String toString() { String sup = super.toString): return sup""+ sup: public class Josh extends Lucas public void b) { print("Josh b...
4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...
C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement the constructors and member function of each of the classes (Marks 15) class Fraction{ private: int numerator; int denominator; public: Fraction(int, int); float fractionValue();//determines the value of numerator/denominator }; class Problem{ private: Fraction f[3]; public: Problem(int, int, int, int, int, int); Fraction largestFraction();//Returns the fraction having largest fraction value }; Question 2: In the following Inheritance problem #include<iostream> #include<string> using namespace std; class Animal{...
A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...
4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...
Why is the assertion failing? what code can fix the problem and where should that code be located at? class Point { public: Point() { myX = myY = 0; } Point(int n, int y) { myx = x; myy = y; } int getX() const { return myX; } int getY() const { return myY; } virtual void read(istream& in) { in >> myX >> myY; } private: int myx, myY; }; class Point3D : Public Point { public:...