Consider Structs
Dec17

Consider Structs

Welcome to CodeSpread! While taking birth in programming world, we all had our own experiences of learning; begin working on a new language or OOPS concepts or experimenting with a new tool or compilers. One of such experiences is already available here. Let’s share one more personal experience and an embarrassing one. J I was just caught up with c# and begin traversing the beautiful journey of programming. I was very overwhelmed with my OOPS knowledge and coding skills. I was making applications, enjoying cups of coffees and playing ping-pong with my team. One day, for a requirement, I was asked to create a program and I created it using lot of meaningless lines of codes. Then during review, I came to know that it could have been done by structs. It was quite embarrassing that I could not recollect structs, just because we do not use it in our daily programming. It was my early phase so was not crucified. Happy go lucky!! I realized that my instant reaction to almost any requirement was class, oops and objects as though it is limited water in the pond. But programming is an ocean. So just to prove that I know structs, I will share structs syntax and knowledge here. What is Structs? A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. [msdn] struct Example { public int x, y; public Example (int x, int y) { this.x = x; this.y = y; } } Few points about Structs: Structs are value types. Struct values are stored on the stack. Inheritance is not supported. Structs can implement interfaces. Structs can improve memory allocations in case of lightweight objects. Structs cannot have a default (parameterless) constructor. Let’s see an example to see structs’ value type nature. For the same struct declared above, we will use it in the below program. Class StructExample { public void AssignStruct(Example instance) { instance.x = 1; instance.y = 2; }   public static void Main() { Example example = new Example (); example.x = 3; example.y = 4; AssignStruct(example); Console.WriteLine(example.x); Console.WriteLine(example.y); } }   The result will be 3 4 Here, struct “Example” being a value type creates a copy and was passed to the AssignStruct method. So value of the struct field remains same in the output and will not have effect of AssignStruct Method. Few lines of code and lightweight object is what the requirement might be, so always consider structs before jumping to class based...

Read More
Why Multiple Inheritance is discouraged?
Dec27

Why Multiple Inheritance is discouraged?

Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for development but what you might not know is the reason as why it is not possible. First of all,we will revisit few basics of inheritance. What is Inheritance? Wikipedia says “In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support.” In simple words, Inheritance forces reusability where a class reuses the functionality of another class by inheriting all its traits.We can take an example to understand more: In real life, We inherit some of our behavior from our parents,so we can deduce our parents as Base class,and ourselves as Derived class. What are the types of Inheritance? Single Level Inheritance: It is the basic type of Inheritance where child class B inherits/extends the behavior of a base class A. For ex: We can have a child class like TV which extends base class like Electronics. Multi Level Inheritance: Here, Single inheritance is captured at different levels and called as multi-level inheritance. We have a derived class C which extends class B, which is a child class of base class A. For ex: Can we say Ipad extends Laptop and Laptop extends Desktop? Multiple Inheritance: The concept of multiple inheritance is a derived class C extends more than one base class like class A and Class B. Multiple inheritance is not supported by many languages and can be achieved by using interfaces. For ex: Child extends behavior of Father and Mother. Now we will move to our basic question, Consider the c++ program mentioned below , where I have created 3 classes,Two of them are base class and third one is the derived class, Base1 and Base2 are Base classes Derive is the Derived class /*Program about multiple inheritance */ class Base1 { public : void Display() { cout<<"We are in Base class1 " ; } } ; class Base2 { public : void Display() { cout<<"We are in Base class2 " ; } } ; class Derive :public Base1,public Base2 { } ; If we focus on Base1 and Base2, then it is clearly visible that, Base1 class has a method called Display Base2 class also has a method called Display, We extend these base classes with our derived class. Let’s move forward and create an instance of derived class, int main () { Derive d; d.Display(); return 0 ; } When we call the Display method,then the...

Read More

Interview Series: Polymorphism Question- Answer Updated

We are sharing our third question of the series in the form of comic, Please help our kid in the comic by sharing your answers through comments or Facebook  or Google plus or twitter[@CodeSpreadBlog]. We have selected answers from Gaurav and Swati as they both were correct. Thanks Guys! Good Luck for the next...

Read More

Concepts: S O L I D

SOLID – Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check these principles for robust design. Today, We will explore these principles, not in terms of definition or meaning but why they are required or how they can help in designing our system. S –Single responsibility principle Wikipedia defines it as “every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class”. The idea behind this principle is that, if you have a class and there are some situation like there are dependent or inherited classes. there is some consumer flow attached to it. So if we go ahead and introduce any change to our main class then this change should only be limited to this class .  None of the dependent classes or consumers should have to be modified to adjust to this change. How and why? By following this principle, we can limit the number of places we have to change the code for a single change anywhere. It can facilitate clean code and division. Other than the advantages mentioned above, following this principle leads to Simple code. understandable design. Maintainability  O –Open/closed principle Wikipedia definition says “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” When we write a code, we ,somewhat  has to predict the possible changes coming , as it is a well-known fact that the code that we are writing is susceptible to change when we are writing it. However if the developer is inexperienced and is not able to predict the possible changes, so to deal with  it ,go ahead and write the simple code. How and why? Although, the moment the changes are requested then he should be ready to introduce this principle and works on the extension of the code and make it more robust. As per the principle, software design should be open for extensions and closed for modifications . This is a tough prediction and involvement of  developer / designer is required to identify what areas of his code are susceptible to change. L – Liskov Substitution Principle Wikipedia definition – “if S is a subtype of T, then objects of type T may be replaced with objects of type S” This line always confused the readers as they talk about interchangeable principle  between base types and sub-types. if sub-types can replace base-types then why do we required base types at all? The primary objective of this design is that the code while calling should not know the difference between the base...

Read More