class-design

When should you use friend classes? [duplicate]

冷暖自知 提交于 2019-12-03 03:45:58
This question already has answers here : When should you use 'friend' in C++? (30 answers) Possible Duplicate: When should you use 'friend' in C++? I have come to a stumbling block because of lack of documentation on friend classes. Most books just explain it briefly, e.g an excerpt from C++: the Complete Reference : Friend Classes are seldom used. They are supported to allow certain special case situations to be handled. And frankly, I have never seen a friend class in any good code made by an experienced C++ programmer. So , here is my list of problems. Do Inherited Classes have the same

Methods in Object-Oriented Design

拥有回忆 提交于 2019-12-03 03:04:37
Q1. In my university studies of object-oriented modelling and design they recommend thinking about what an object can do for its method, and what its responsibilities are for its attributes. All attempts at clarification have resulted in further confusion. This tends to generate a class diagram with actors who have all the actions, and inner classes which only hold data. This doesn't seem correct. Is there another way of thinking about how to model the objects? Q2. Also, the course seems to emphasize modelling the objects after their real-world counterparts but it doesn't necessarily make

USE case to Class Diagram - How do I?

谁说我不能喝 提交于 2019-12-03 02:50:00
问题 I would like your guidance on how to create classes and their relationships (generalization, association, aggregation and composition) accurately from my USE case diagram (please see below). I am trying to create this class diagram so I can use it to create a simple online PHP application that allows the user to register an account, login and logout, and store, search and retrieve data from a MySQL database. Are my classes correct? Or should I create more classes? And if so, what classes are

How to force only smart pointers instance for a class?

纵然是瞬间 提交于 2019-12-03 02:33:34
I've been working on a way to prevent user of using a class without smart pointers. Thus, forcing them to have the object being heap allocated and managed by smart pointers. In order to get such a result, I've tried the following : #include <memory> class A { private : ~A {} // To force use of A only with std::unique_ptr friend std::default_delete<A>; }; This work pretty well if you only want your class users being capable of manipulating instance of your class through std::unique_ptr . But it doesn't work for std::shared_ptr . So I'd like to know if you had any ideas to get such a behavior.

How can a singleton class use an interface?

好久不见. 提交于 2019-12-03 01:09:01
I read at many places that singletons can use interfaces. Some how I am unable to comprehend this. Every class can implement an interface, and a Singleton is just a "normal" class that makes sure that only one instance of it exists at any point in time apart from the other business logic it may implement. This also means that a Singleton has at least 2 responsibities and this is not good OO design as classes should only have 1 responsibility and make sure they are good at that responsibility, but that is another discussion. Something like: public interface MyInterface { } And public class

Large scale usage of Meyer's advice to prefer Non-member,non-friend functions?

不羁岁月 提交于 2019-12-03 00:57:30
For some time I've been designing my class interfaces to be minimal, preferring namespace-wrapped non-member functions over member functions. Essentially following Scott Meyer's advice in the article How Non-Member Functions Improve Encapsulation . I've been doing this with good effect in a few small scale projects, but I'm wondering how well it works on a larger scale. Are there any large, well regarded open-source C++ projects that I can take a look at and perhaps reference where this advice is strongly followed? Update: Thanks for all the input, but I'm not really interested in opinion so

Is there a rule of thumb for when to code a static method vs an instance method?

≡放荡痞女 提交于 2019-12-03 00:30:17
I'm learning Java (and OOP) and although it might irrelevant for where I'm at right now, I was wondering if SO could share some common pitfalls or good design practices. One important thing to remember is that static methods cannot be overridden by a subclass. References to a static method in your code essentially tie it to that implementation. When using instance methods, behavior can be varied based on the type of the instance. You can take advantage of polymorphism. Static methods are more suited to utilitarian types of operations where the behavior is set in stone. Things like base 64

How do I design a class in Python?

℡╲_俬逩灬. 提交于 2019-12-02 23:59:58
问题 I've had some really awesome help on my previous questions for detecting paws and toes within a paw, but all these solutions only work for one measurement at a time. Now I have data that consists off: about 30 dogs; each has 24 measurements (divided into several subgroups); each measurement has at least 4 contacts (one for each paw) and each contact is divided into 5 parts and has several parameters, like contact time, location, total force etc. Obviously sticking everything into one big

What's wrong with Copy Constructors? Why use Cloneable interface?

牧云@^-^@ 提交于 2019-12-02 23:13:06
When programming C++ we used to create copy constructors when needed (or so we were taught). When switching to Java a few years ago, I noticed that the Cloneable interface is now being used instead. C# followed the same route defining the ICloneable interface. It seems to me that cloning is part of the definition of OOP. But I wonder, why were these interfaces created, and the copy constructor seems to have been dropped? When I thought about it, I came to the thought that a copy constructor would not be useful if one needs to make a copy of an object whose type is not known (as in having a

How do I break my procedural coding habits? [closed]

跟風遠走 提交于 2019-12-02 19:13:27
I recently read an interesting comment on an OOP related question in which one user objected to creating a "Manager" class: Please remove the word manager from your vocabulary when talking about class names. The name of the class should be descriptive of its' purpose. Manager is just another word for dumping ground . Any functionality will fit there. The word has been the cause of many extremely bad designs This comment embodies my struggle to become a good object-oriented developer. I have been doing procedural code for a long time at an organization with only procedural coders. It seems like