I am learning the OOP recently and i have a few questions to ask

1) When should i use operator overloading?

2) When should i use template?

Recommended Answers

All 7 Replies

Operator overloading is used to make it easier to read/use an object. For example, you can overload the operator << so that you can print the value associated with an object rather than using an accessor function.

Operator overloading also allows you to assign one object to another via the operator =. This ensures that everything is copied correctly from one object to another.

Templates, I'm just starting on them, but they are used when you want to make a container (or object) like a vector which can handle any type (int, double, string, whatever).

Templates and operator overloading are just tools like everything else in C++ - Use them when they're appropriate. If you understand what they're capable of, you'll be able to recognise for yourself where and when they might be useful; but if you'd like some specific examples, then look no further than the STL

>I am learning the OOP recently and i have a few questions to ask
I wouldn't list operator overloading or templates as features of OOP. In fact, templates are how C++ supports a somewhat different paradigm from OOP: generic programming.

>1) When should i use operator overloading?
>2) When should i use template?

Bench already mentioned this, but I'll emphasize it. If you have to ask this question, you probably don't understand the feature well enough to use it properly in real code. If you do understand the feature well enough to use it, you'll also know intuitively when it should be used.

And of course, moderation is key. I've seen horrible messes where the programmer thought operator overloading was cool and used it too much. Template metaprogramming is a fantastic example of templates gone wrong. Is it cool? Absolutely. Is it occasionally useful? Sure. Is it an everyday thing? Hell no.

whats is a default constructor and paramiterlize constructor

I am learning the OOP recently and i have a few questions to ask

1) When should i use operator overloading?

2) When should i use template?

1) it is a concept of polymphim for example

int sum(int)
float sum(float)
long sum (long)

okay~thx for reply ^^..i have to study more detail about it

Operator overloading also allows you to assign one object to another via the operator =. This ensures that everything is copied correctly from one object to another.

Yeah. Look into deep and shallow copies, and what they mean, and how/when to use Copy Constructors.

Overloading the = operator and having a copy constructor are required when you have classes which contain dynamic memory (pointers) as you want to copy those values as well, not just there reference.

2) When should i use template?

For parametric polymorphism. E.g you may want to build a stack which can hold any datatype (the STL's inbuilt vector etc.... datastructures do this)

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.