oops concept on c#

27

Upload: baabtracom-mentoring-partner-first-programming-school-in-india

Post on 19-Jan-2015

9.960 views

Category:

Education


1 download

DESCRIPTION

Oops concept on c#

TRANSCRIPT

Page 1: Oops concept on c#
Page 2: Oops concept on c#

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Oops concept on c#

Week Target Achieved

1 50 19

2 50 22

3 50

Typing Speed

Page 4: Oops concept on c#

Jobs Applied# Company Designation Applied Date Current Status

1

2

3

Page 5: Oops concept on c#

Oops Concept in C#

Nabeelnabilmohad@gmail.comnabilmohadnabilmohadnabilmohad9746477551

Page 6: Oops concept on c#

POP OOP

In POP, program is divided into small parts called functions.

In OOP, program is divided into parts called objects.

POP does not have any proper way for hiding data so it is less secure.

OOP provides Data Hiding so provides more security.

Example of POP are : C, VB, FORTRAN, Pascal.

Example of OOP are : C++, JAVA, VB.NET, C#.NET.

Page 7: Oops concept on c#

Oops Concept in C#

• Object Oriented Programming language(OOPS):-

It is a methodology to write the program where we specify the code in form of classes and objects.

Page 8: Oops concept on c#

Class

• Class is a user defined data type. it is like a template. In c# variable are termed as instances of classes. which are the actual objects.

Class classname { variable declaration; Method declaration; }

Page 9: Oops concept on c#

Object• Object is run time entity which has different attribute to identify it

uniquely.

Rectangle rect1=new rectangle(); Rectangle rect1=new rectangle();

Here variable 'rect1' & rect2 is object of the rectangle class. The Method Rectangle() is the default constructor of the class. we can

create any number of objects of Rectangle class.

Page 10: Oops concept on c#

Basic Concept of oops:-

• There are main three core principles of any object oriented languages.

• INHERITANCE• POLYMORPHISM• ENCAPSULATION

Page 11: Oops concept on c#

INHERITANCE:-

• One class can include the feature of another class by using the concept of inheritance.In c# a class can be inherit only from one class at a time.Whenever we create class that automatic inherit from System.Object class,till the time the class is not inherited from any other class.

Page 12: Oops concept on c#

• class BaseClass• {• //Method to find sum of give 2 numbers• public int FindSum(int x, int y)• {• return (x + y);• }• //method to print given 2 numbers• //When declared protected , can be accessed only from inside the derived class• //cannot access with the instance of derived class• protected void Print(int x, int y)• {• Console.WriteLine("First Number: " + x);• Console.WriteLine("Second Number: " + y);• }• }

Page 13: Oops concept on c#

• class Derivedclass : BaseClass• {

• public void Print3numbers(int x, int y, int z)• {• Print(x, y); //We can directly call baseclass members• Console.WriteLine("Third Number: " + z);• }

• }

Page 14: Oops concept on c#

• class Program• {• static void Main(string[] args)• {• //Create instance for derived class, so that base class members • // can also be accessed• //This is possible because derivedclass is inheriting base class

• Derivedclass instance = new Derivedclass();

• instance.Print3numbers(30, 40, 50); //Derived class internally calls base class method.• int sum = instance.FindSum(30, 40); //calling base class method with derived class instance• Console.WriteLine("Sum : " + sum);

• Console.Read();• }• • }

Page 15: Oops concept on c#

• Output:-• First number:30• Second number:40• Third number:50• Sum:70

Page 16: Oops concept on c#

ENCAPSULATION:-• Encapsulation is defined 'as the process of enclosing one or more items

within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.

• Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.

• Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers:

• Public• Private• Protected

Page 17: Oops concept on c#

POLYMORPHISM:-

• It is also concept of oops. It is ability to take more than one form. An operation may exhibit different behaviour in different situations.

• C# gives us polymorphism through inheritance. Inheritance-based polymorphism allows us to define methods in a base class and override them with derived class implementations

• You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.

Page 18: Oops concept on c#

Function Overloading

• class Printdata• {• void print(int i)• {• Console.WriteLine("Printing int: {0}", i);• }

• void print(double f)• {• Console.WriteLine("Printing float: {0}", f);• }

• void print(string s)• {• Console.WriteLine("Printing string: {0}", s);• }• static void Main(string[] args)• {• Printdata p = new Printdata();• // Call print to print integer• p.print(5);• // Call print to print float• p.print(500.263);• // Call print to print string• p.print("Hello C++");• Console.ReadKey();• }• }

Page 19: Oops concept on c#

• Output:-• Printing int: 5• Printing float: 500.263 • Printing string: Hello C++

Page 20: Oops concept on c#

Override• class Color• {• public virtual void Fill()• {• Console.WriteLine("Fill me up with color");• }• public void Fill(string s)• {• Console.WriteLine("Fill me up with {0}", s);• }• }

• class Green : Color• {• public override void Fill()• {• Console.WriteLine("Fill me up with green");• }• }

• class nab• {• static void Main()• {• Green c1 = new Green();• c1.Fill();• c1.Fill("red");• Console.Read();• }• • }• Out put:• Fillup with Green• Fillup with Red

Page 21: Oops concept on c#

Dynamic Polymorphism

• C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods which are implemented by the derived class. The derived classes have more specialized functionality.

• Please note the following rules about abstract classes:• You cannot create an instance of an abstract class• You cannot declare an abstract method outside an

abstract class• When a class is declared sealed, it cannot be inherited,

abstract classes cannot be declared sealed.

Page 22: Oops concept on c#

• abstract class Shape• {• • public abstract int area();• • }• class Rectangle: Shape• {• private int length;• private int width;• public Rectangle( int a, int b)• {• length = a;• width = b;• }• public override int area ()• { • Console.WriteLine("Rectangle class area :");• return (width * length); • }• }

• class RectangleTester• {• static void Main(string[] args)• {• Rectangle r = new Rectangle(10, 7);• double a = r.area();• Console.WriteLine("Area: {0}",a);• Console.ReadKey();• }• }• Out put:• Rectangle class area :• Area:70

Page 23: Oops concept on c#

Abstract Classes and Class Members

• The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

• Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:

Page 24: Oops concept on c#

sealed

• When applied to a class, the sealed modifier prevents other classes from inheriting from it.

• When you define new methods or properties in a class, you can prevent deriving classes from overriding them by not declaring them as virtual.

Page 25: Oops concept on c#
Page 26: Oops concept on c#

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 27: Oops concept on c#

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]