With this post I'm gonna have brief cover on the some important Object Oriented Concepts with the help of C# and C++ coding.
Inheritance
If the object or a Class( take it as A) that based on another class( take it as B) we called A inherits B or A is a child of B. It allows to use permitted properties and methods of parent without implementing them inside the children with in children.
C++
C#
Unfortunately C# doesn't support for multiple inheritance. To archive multiple inheritance functionality there there is way with interfaces. Hoping to add some separate article about interfaces.
Magical Static
Static is one of important keyword that programmers using rapidly. static has two meanings with the usage of it.
if static keyword is using with variable in the code that means there is only one instance of that variable through out the execution time.
if static keyword is using with method it means that method can be used without create any instance of class or outside the class.
static members are not bounded with the class.
C++
C#
Access Modifiers and Key Words
Access modifiers defines the accessibility level of Class, Property or Method .
There are three basic access modifiers
But with C# there is an additional access modifier called internal this type or member can be accessed by any code in the same solution/namespace, but not from another solution/namespace.
and C# allows to use internal protected type also.
Those access modifiers are applicable for classes also
C#
Abstract, Sealed and Partial are another useful keywords that can be using with Class and Methods in OOP in C#.
Abstract modifier indicates that the thing being modified has a missing or incomplete implementation.It indicates that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
C#
Sealed modifier prevents other classes from inheriting from it. It seals the class.
Partial modifier can be used only with Classes and Methods. It makes available to split code among few classes or code pages.
C#
Polymorphism (Overloading and Overriding)
Polymorphism is a Greek word that means "many-shaped". There is two types of Polymorphism. that is
Overloading is when you have multiple methods in the same scope, with the same name but different signatures.
C++
C#
Operator Overloading
This feature is not available with Java. It allows you to change the functionality of operators with our custom arguments. But at the end it should do the similar kind of function that did previously
Scenario : Assume that we having class called liquor , and we have two instances of that Class called liquor1 and liquor2. this liquor1+liquor2 should will return total addition of all properties that liquor Class got. In here we need to overload the Operator +
Here is the Operators that can be overload
+ - * / % | & ~ ! = < > += -= *= /= %= |= &= << >> <<= >>= == != <= >= && || ++ -- , -> *-> [ ] ( ) new delete
C++
C#
Overriding is a principle that allows you to change the functionality of a method in a child class.
C++
OR
C#
The virtual keyword is used to
modify a method, property, indexer, or event declaration and allow for
it to be overridden in a derived class.
Inheritance
If the object or a Class( take it as A) that based on another class( take it as B) we called A inherits B or A is a child of B. It allows to use permitted properties and methods of parent without implementing them inside the children with in children.
C++
class
A
{
public
int
Num;
};
//Child Class
class
B : A
{
public
string name;
};
class
C
{
public
string email;
};
//Multiple inheritance
class
D : A,C
{
};
C#
class
A
{
public
int
Num { get; set; }
}
class
B:A
{
public
string name { get; set; }
}
Magical Static
Static is one of important keyword that programmers using rapidly. static has two meanings with the usage of it.
if static keyword is using with variable in the code that means there is only one instance of that variable through out the execution time.
if static keyword is using with method it means that method can be used without create any instance of class or outside the class.
static members are not bounded with the class.
C++
class
demo
{
public
:
static
int
staticVariable;
static
int
StaticFuntion();
};
//set the value for static variable
int
demo::staticVariable = 10;
int
demo::StaticFuntion()
{
return
3;
};
int
main()
{
cout<<demo::staticVariable<<
"\n"
;
cout<<demo::StaticFuntion();
}
C#
public
class
Demo
{
public
static
int
staicNumber {
get
;
set
; }
public
static
int
staticMethod()
{
return
3;
}
}
class
Demo2
{
public
void
funtion()
{
MessageDialog ms =
new
MessageDialog(Demo.staticMethod().ToString());
ms.ShowAsync();
MessageDialog ms2 =
new
MessageDialog(Demo.staicNumber.ToString());
ms2.ShowAsync();
}
}
Access Modifiers and Key Words
Access modifiers defines the accessibility level of Class, Property or Method .
There are three basic access modifiers
- Public
- Private
- Protected
class
demo
{
public
:
int
staticVariable;
int
StaticFuntion();
private
:
int
staticVariable1;
int
StaticFuntion1();
protected
:
int
staticVariable2;
int
StaticFuntion3();
};
But with C# there is an additional access modifier called internal this type or member can be accessed by any code in the same solution/namespace, but not from another solution/namespace.
and C# allows to use internal protected type also.
Those access modifiers are applicable for classes also
C#
public
class
Demo
{
public
int
MyProperty {
get
;
set
; }
private
int
MyProperty1 {
get
;
set
; }
protected
int
MyProperty2 {
get
;
set
; }
internal
int
MyProperty3 {
get
;
set
; }
internal
protected
int
MyProperty4 {
get
;
set
; }
}
Abstract, Sealed and Partial are another useful keywords that can be using with Class and Methods in OOP in C#.
Abstract modifier indicates that the thing being modified has a missing or incomplete implementation.It indicates that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
C#
public
class
Demo
{
abstract
public
int
addition();
}
public
class
Demo2:Demo
{
public
override
int
addition()
{
return
10;
}
}
Sealed modifier prevents other classes from inheriting from it. It seals the class.
Partial modifier can be used only with Classes and Methods. It makes available to split code among few classes or code pages.
C#
public
partial
class
Sample
{
public
void
Method1()
{
}
}
public
partial
class
Sample
{
public
void
Method2()
{
}
}
public
sealed
class
Demo2 : Demo
{
private
void
GiveExample()
{
Sample Object =
new
Sample();
Object.Method1();
Object.Method2();
}
}
Polymorphism (Overloading and Overriding)
Polymorphism is a Greek word that means "many-shaped". There is two types of Polymorphism. that is
- Overloading.
- Overriding.
Overloading is when you have multiple methods in the same scope, with the same name but different signatures.
C++
class
Demo
{
public
:
void
Function(
int
i) {
}
void
Function(string name) {
}
};
C#
//Overloading
public
class
Demo
{
public
void
Function
(
int
id)
{}
public
void
Function
(
string
name)
{}
}
Operator Overloading
This feature is not available with Java. It allows you to change the functionality of operators with our custom arguments. But at the end it should do the similar kind of function that did previously
Scenario : Assume that we having class called liquor , and we have two instances of that Class called liquor1 and
Here is the Operators that can be overload
+ - * / % | & ~ ! = < > += -= *= /= %= |= &= << >> <<= >>= == != <= >= && || ++ -- , -> *-> [ ] ( ) new delete
C++
class
liquor
{
public
:
int
capacity;
string name;
liquor operator+(liquor l1);
};
liquor liquor:: operator+(liquor l1)
{
liquor ans;
ans.capacity= l1.capacity + capacity;
ans.name= (l1.name +
" "
+ name);
return
ans;
}
void
Main()
{
liquor liquor1;
liquor1.capacity = 10;
liquor1.name=
"Vodka"
;
liquor liquor2;
liquor2.capacity = 20;
liquor2.name =
"Vine"
;
liquor liquor3 = liquor1 + liquor2;
}
C#
public
class
liquor
{
public
int
capacity {
get
;
set
; }
public
string
name {
get
;
set
; }
//overload the + Operator
public
static
liquor
operator
+(liquor l1, liquor l2)
{
liquor ans =
new
liquor(){capacity= l1.capacity + l2.capacity,name= (l1.name +
" "
+ l2.name)};
return
ans;
}
}
public
class
addition
{
//usage function
public
void
returnAdition()
{
liquor liquor1 =
new
liquor() { capacity=10, name=
"Vodka"
};
liquor liquor2 =
new
liquor() { capacity = 20, name =
"Vine"
};
liquor liquor3 = liquor1 + liquor2;
}
}
Overriding is a principle that allows you to change the functionality of a method in a child class.
C++
class
A
{
public
:
virtual
void
fn(){cout<<10;};
//pure virtual member function
};
class
B:A
{
public
:
void
fn()override{cout<<40;}
//Overridden
};
class
A
{
public
:
void
fn(){cout<<10;};
//pure virtual member function
};
class
B:A
{
public
:
void
fn(){cout<<40;}
//Overridden
};
C#
public
class
Demo
{
public
virtual
void
Method2()
{
//Print functionality implemented
}
}
public
class
Demo2 : Demo
{
public
override
void
Method2()
{
//Print functionality implemented with some operation
}
}
0 comments:
Post a Comment