اذهب إلى المحتوى

السؤال

نشر

Write a C++ class and call it SumProd. The SumProd class should have two public fields 
of type double named a and b and one private field of type double named c.
The SumProd class should have default constructor that initializes all its fields to zeros 
and prints (default SumProd constructor called) and another constructor that initializes all
its fields to values other than zero.
The SumProd class should also contain a method that returns the sum and of its fields
and another method that returns the product and of its fields.
Create an instance of the SumProd class and initializes its fields to 2, 4 and 6. Then, print 
its c field, the sum and product (call the methods you wrote in the class) of its fields.

Recommended Posts

  • 1
نشر

مرحباً بك،

يمكن تنفيذ المطلوب كالآتي:

#include <iostream>

using namespace std;

class SumProd {
    private:
        double c;
    public:
        double a, b;
        SumProd(){
            a = 0;
            b = 0;
            c = 0;
            cout << "default SumProd constructor called";
        }
        SumProd(double a_val, double b_val, double c_val) {
            a = a_val;
            b = b_val;
            c = c_val;
        }
        double sum(){
            return a + b + c;
        }
        double product(){
            return a * b * c;
        }
        void print_c(){
            cout << c << endl;
        }
};

int main()
{
    SumProd obj(2, 4, 6);
    obj.print_c();
    cout << obj.sum() << endl;
    cout << obj.product() << endl;
    return 0;
}

 

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...