I've never programed in C++, and I have just started to learn C++, but I already get a little bit to complicated homeassignement. Is there anyone willing to help me - let's say solve me this excercise. I would appreciate any help with this and if there is anyone willing to solve the whole program, the point is in understanding it for me, so can it be explained, please.

These are 2 connected problems:


4. Define a class MobilePhone for representing a mobile phone that is given by its device
name, e.g., Motorola RAZR V3, the owner's name, and the telephone number. To that end,
please declare three data elements of type string. Furthermore define the following inline
methods:

– a constructor that initialises each data element,
(please use default values for the owner's name and the telephone number)
– an access method for each data element,
– a method display() that outputs the data elements.
Methods that access an object by reading shall be declared read-only.


5. Two objects of type MobilePhone are passed to a global function
exchangeMobilePhone() as arguments. The function exchanges the names of the owners
and the telephone numbers.

Dani AI

Generated

As requested a runnable solution and already sketched the storage, below is a compact, complete example that implements the constructor (with defaults), const accessors, an inline display() and a global exchangeMobilePhone() that swaps owners and numbers. 's point about doing the work is fair: type this in, step through it, then tweak it to learn why each piece is needed.

#include <iostream>
#include <string>

class MobilePhone {
public:
    MobilePhone(const std::string& model,
                const std::string& owner = "Unknown",
                const std::string& number = "N/A")
      : model_(model), owner_(owner), number_(number) {}

    const std::string& model() const { return model_; }
    const std::string& owner() const { return owner_; }
    const std::string& number() const { return number_; }

    void setOwner(const std::string& o) { owner_ = o; }
    void setNumber(const std::string& n) { number_ = n; }

    void display(std::ostream& os = std::cout) const {
        os << "Model: " << model_ << '\n'
           << "Owner: " << owner_ << '\n'
           << "Number: " << number_ << '\n';
    }

private:
    std::string model_;
    std::string owner_;
    std::string number_;
};

void exchangeMobilePhone(MobilePhone& a, MobilePhone& b) {
    std::string tmp = a.owner();
    a.setOwner(b.owner());
    b.setOwner(tmp);

    tmp = a.number();
    a.setNumber(b.number());
    b.setNumber(tmp);
}

Notes and common pitfalls: methods that only read the object are marked const so callers can use const instances. Pass the phones by non-const reference to exchangeMobilePhone() — passing by value swaps copies and leaves the originals unchanged. Use const std::string& in parameters to avoid unnecessary copies; consider std::move for rvalue optimization if you learn moves later.

Recommended Answers

All 3 Replies

> and if there is anyone willing to solve the whole program,
Anything else you want while you're at it?
World peace, large trust fund, that kinda stuff.

The moment you give up, and post this kind of thread to a forum is the time you should quit.

> but I already get a little bit to complicated homeassignement
You ain't seen nothing yet. The problems which are coming are much harder than this. Are you going to post all those to some forum as well?
You have to work damn hard to keep on top of this stuff.

Understanding comes from you struggling with the problem and getting occasional help to specific problems.

If you think you'll "understand" just because someone else posts an answer, and you can recite a few memorised sentences for teacher, then you're SOL.

Make an effort!

I understand what are you saying to me, but I cannot make so much effort to solve this by tomorrow, and to completely understand it and to present it, that's why I need someon's help.

Member Avatar for Member #248612

4. Define a class MobilePhone for representing a mobile phone that is given by its device
name, e.g., Motorola RAZR V3, the owner's name, and the telephone number. To that end,
please declare three data elements of type string.

Just do what you read! Here a litte help:

class MobilePhone
{
    std::string device_name_;
    std::string owners_name_;
    std::string phone_number_;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.