Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6663043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:27:22+00:00 2026-05-26T02:27:22+00:00

I need to represent Volts, Ampers, And Watts and their relations (e.g. W =

  • 0

I need to represent Volts, Ampers, And Watts and their relations (e.g. W = V * I)..

This is what I came with, but it seems really verbose. Any idea on how to make it more concise ?

#include <stdio.h>
#include <iostream>


template<class T>    
class double_val {   
public:
    explicit double_val(double val):_val(val) { };
    double_val(const T& other) {
        _val = other._val;
    }
    T& operator=(const T &other) {
        _val = other._val;
        return *this;
    }
    T operator+ (const T& other) const {
        return T(this->_val + other._val);
    }
    T operator+ (double val) const {
        return T(this->_val + val);
    }
    T operator- (const T& other) const {
        return T(this->_val - other._val);
    }
    T operator- (double val) const {
        return T(this->_val - val);
    }
    T operator* (const T& other) const {
        return T(this->_val * other._val);
    }
    T operator* (double val) const {
        return T(this->_val * val);
    }
    T operator/ (const T& other) const {
        return T(this->_val / other._val);
    }
    T operator/ (double val) const {
        return T(this->_val / val);
    }
    bool operator== (const T& other) const{
        return this->_val == other._val;
    }
    bool operator!= (const T& other) const{
        return this->_val != other._val;
    }
    bool operator > (const T& other) const{
        return this->_val > other._val;
    }
    bool operator >= (const T& other) const{
        return this->_val >= other._val;
    }
    bool operator < (const T& other) const{
        return this->_val < other._val;
    }
    bool operator <= (const T& other) const{
        return this->_val <= other._val;
    }
    void val(double val){
        _val = val;
    }
    double val() const {
        return _val ;
    }
    virtual const char* name() const = 0;

private:
    double _val;
};
template<class T>
std::ostream& operator<<(std::ostream &os, const double_val<T> &t) {
    return os << t.val() <<  " " << t.name();
}


class Amper:public double_val<Amper> {
public:
    Amper(double val):double_val<Amper>(val) {}
    const char* name() const {
        return "Amper";
    }
};

class Volt:public double_val<Volt> {
public:
    Volt(double val):double_val<Volt>(val) {}
    const char* name() const {
        return "Volt";
    }
};

class Watt:public double_val<Watt> {
public:
    Watt(double val):double_val<Watt>(val) {}
    const char* name() const {
        return "Watt";
    }
};

// Watt = I * V
Watt operator* (const Volt &v, const Amper& a) {
    return Watt(a.val() * v.val());
}
Watt operator* (const Amper &a, const Volt& v) {
    return Watt(a.val() * v.val());
}

// Volts = Watts/Ampers
Volt operator / (const Watt &w, const Amper& a) {
    return Volt(w.val() / a.val());
}

// Ampers = Watts/Volts
Amper operator / (const Watt &w, const Volt& v) {
    return Amper(w.val() / v.val());
}



int main(int argc, char **argv) {
    using namespace std;
    Watt w = Volt(66) * Amper(7);
    Amper a = w / (Volt(646) * Volt(444));

    cout << a << endl;

    return 0;
}

(note: I intentionaly omitted operator double(), since I wanted to avoid forbidden ops like Volt(4) + Watt(6))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:27:23+00:00Added an answer on May 26, 2026 at 2:27 am

    There is indeed a less verbose way in library form, Boost.Units which defines the SI units as type safe class template instantiations.

    In particular, it defines the units volt(s), ampere(s) and watt(s).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to represent huge numbers in our application. We're doing this using integer
I have 6000 data of district, subdistrict. I need to represent this on dependent
Need to represent a two column table. Maybe just for reference at this stage
I have a floating point value, but when i need to represent to the
I need to represent this hierarchy in a JSON object. Can someone help me
I need to represent a table containing data about some tests and their results.
I have a list of N colors. I need to represent any of those
I need to represent calculator key presses by the text for the keys to
I need to represent an IEEE 754-1985 double (64-bit) floating point number in a
I need to represent immutable vectors in Python (vectors as in linear algebra, not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.