Posts

Showing posts with the label oop

Exception member class OOP

Exception member class OOP I have met the following concept in the production code: class A { public: class Exception : public std::exception {/* ... */}; //... }; Nobody can give me a clear answer why it is like this. My question is whether this approach is in line with SOLID rules? I think that would be better if this exception class is located outside the class A and is injected while creating the instance of A. Design principles are important, but they aren't followed "because they are design principles". They are followed because they accomplish something. What would injecting the exception object upon construction accomplish? Now the class should manage this objects lifetime. What if an error state is never encountered, and it's never thrown? – StoryTeller Jul 2 at 6:16 ...

What is the difference between @property and getattr()? [closed]

What is the difference between @property and getattr()? [closed] What is the difference between @property, @property.setter and getattr(), setattr(). When will I use them? If both are the same, which one is preferred to use in python 3.x? I am new to python and not understanding where to use and how to use them with OO python I have gone through many websites, but I'm not sure which one to use. Please give me some real time example. Appreciate your help. EX: getattr() class emp: name='Harsh' salary='25000' def show(self): print self.name print self.salary e1 = emp() print getattr(e1,'name') setattr(e1,'height',152) @ property class P: def __init__(self,x): self.x = x @property def x(self): return self.__x @x.setter def x(self, x): if x < 0: self.__x = 0 elif x > 1000: self.__x = 1000 else: self.__x = x Please edit the question to limit it to a specific problem with enough d...

C++ downgrade parent pointer to child pointer used as function parameter

C++ downgrade parent pointer to child pointer used as function parameter I want use child pointer in place of parent parameter in some function. Here is simplified code #include <iostream> #include <string> using namespace std; class Parent { private: string value; public: void SetValue(const string& in){ value = in; } const string& GetValue() const { return value; } virtual ~Parent() {}; }; class Child : public Parent { }; void Func(Parent* obj) { Child* cobj = dynamic_cast<Child*>(obj); cobj->SetValue("new value"); delete cobj; } int main(int argc, char** argv) { Child* cobj = new Child(); Func(cobj); cout<<"value: "<<cobj->GetValue(); return 0; } But it returns value: Func does not set the value property. How can I fix the code? UPDATE1: But with this code: value: Func #include <iostream> #include <string> using namespace std; class Parent...

How to use class variables?

How to use class variables? A simple question. If I have class A with methods m1 and m2 . Is it a good practice that m1 changes the state of A . And then when m2 is called it relies on A 's state being changed by m1 . In other words if m2 is dependent on m1 being called before m2 being called. With m1 and m2 being both private. Worded differently is it O.K. for private methods to consider the object state as shared memory? Problem arises when they are called out of order. But with the benefit of not having to copy arguments. Any advice? 1 Answer 1 Your description is not a good design. Class variables should have class invariants that are true before and after any (and all) method invocations. The order of calls should not matter. The way you described it make is sounds that there will be cases during which this will not be true, for example if m1 is not called yet and m2 is. Moreover...

Implement some method in base class and some method in derived class of one interface

Implement some method in base class and some method in derived class of one interface We have one interface with four normal methods say method1,method2,method3,methid4 and we want to implement only two methods in base class and the other two in derived classes.how we achieve this? Please provide a Minimal, Complete, and Verifiable example. Please look at guide how do I ask a good question? – Andrzej Ziółek Jul 1 at 11:01 Just a brief we have an interface with four methods , and we have a base class that inheriting that interfaces means we have to implement all method details in base class then we have an other class derived class inheriting from interface class then by default the derived class also have all base methods , so the main objective here to show only method1 and method 2 in base class and method 3 an...