Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ppt of c++ vs c#


Published on


Published in: Education, Technology
  • Be the first to comment

Ppt of c++ vs c#

  2. 2. INTRODUCTION C++ was written by Bjarne Stroustrup at Bell Labs during 1983-1985. C++ is an extension of c. C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg.
  3. 3. OOPs CONCEPT IN C++ AND C++ The basic concepts of oops used in c++ and c# are as follows: Objects Classes Data abstraction Encapsulation Inheritance Polymorphism
  4. 4. OBJECTS When a program is executed, the objects interact by sending messages to one another. For example, if ‘customer’ and ‘account’ are two objects in a program, then the customer object may send message to account object requesting for a bank balance. Each object contains data and code to manipulate data. CLASS objects contain data and function or code to manipulate that data. The entire set of data and code of an object can be made a user-defined data type with the help of a class. In fact objects are variables of type class. Once a class has been defined, we can create any number of objects associated with that class.
  5. 5. PROGRAM TO ADD TWO NUMBERS USING CLASS IN C++ #include<iostream.h> #include<conio.h> class add { public: void sum(int,int); }; void add::sum(int a,int b) { int s; s=a+b; cout<<”sum is:”<<s; } void main() { add ob1; ob1.sum(6,8); getch(); } Output: sum is:14 Member function Object ob1 of class add
  6. 6. CLASS IN C# The class is the most important element in C# because everything must be included inside a class . Unlike other languages, the C# compiler doesn’t allow you to declare any variables or methods outside a class. In fact, object-oriented programming is based on the concept of classes. Structs are similar to classes in some aspects and can be used instead of classes in some applications. The main difference between a class and a struct is that the class is a reference type, while the struct is a value type.
  7. 7. CLASS DECLARATION We declare a class by using the keyword class as follows: class MyClass { // Class implementation } The class implementation (or class body) goes between the braces ({}).
  8. 8. FIELD INITIALIZATION In the following example, we declare the class Point, which contains two fields — x and y: class Point { int x; int y; } We can initialize the fields with values like this example: class Point { int x = 0; int y = 0; } This declaration initializes the fields x and y to their default values.
  9. 9. CLASS INSTANTIATION To create objects from the Point class, use the following statement: Point myPoint; // create the object We can also create the object and initialize the fields in the same statement: Point myPoint = new Point(); In the second statement, when the object is created the default constructor is invoked to initialize fields to their default values. Then we can assign each object its own coordinates: p1.x = 15; p1.y = 22;
  10. 10. Using system; class A { private string name; private void show() { console.write(“welcome to c#”); } public static void main(string [ ] arg) { A obj=new a();[0];;} } Class name field method Method body Class method Local variable Method invocation Method parameters CLASS EXAMPLE
  11. 11. C#: MAIN METHOD() Main must – be named Main – return void or int – be static – have (void) or (string[]) parameters public static void Main() { // Main } public static int Main(string[] args) { // Main return result_code; } public static void Main(string[] args) { // Main }
  12. 12. PROGRAM TO ADD TWO NUMBERS USING CLASS IN C# using System; namespace add_num { class sum { static void Main(string[] args) { int a, b,c; Console.WriteLine(&quot;Enter the first no. :&quot;); a = Int32.Parse(Console.ReadLine()); Console.WriteLine(&quot;Enter the second no :&quot;); b = Int32.Parse(Console.ReadLine()); c = a + b; Console.WriteLine(&quot;total :&quot; + c); Console.ReadLine(); } } } Output: Enter the first no.:6 Enter the second no.:8 total: 14
  13. 13. DATA ABSTRACTION Abstraction refers to the act of representing essential features without including the background details. The concept of abstraction relates to the idea of hiding data that are not needed for presentation. The main idea behind data abstraction is to give a clear separation between properties of data type and the associated implementation details. ENCAPSULATION Encapsulation is the most basic concept of OOP. It is the way of combining both data and the functions that operate on that data under a single unit. The only way to access the data is provided by the functions .These functions are considered as member functions in C++. It is not possible to access the data directly.
  14. 14. INHERITANCE I t is the capability to define a new class in terms of an existing class. An existing class is known as a base class and the new class is known as derived class. Inheritance can be of following types: A B A B C A B C D SINGLE INHERITANCE MULTIPLE INHERITANCE HIEARCHICAL INHERITANCE
  16. 16. SINGLE INHERITANCE If a class is derived from a single base class, it is called as single inheritance. SYNTAX: class derived_class_name : access_specifier base_class_name { data members of the derived class ; member functions of the derived class ; }; The colon (:), indicates that the class derived_class_name is derived from the class base_class_name. The access_specifier may be public, private or protected. If no access_specifier is specified, it is private by default. The access_specifier indicates whether the members of the base class are privately derived or publicly derived.
  17. 17. MULTIPLE INHERITANCE If a class is derived from more than one base class, it is known as multiple inheritance SYNTAX class derived-class-name : access_specifier Baseclass1, access_specifier Baseclass2, ……… …… .. access_specifier Baseclass_n { members of the derived class ; };
  18. 18. Hierarchical Inheritance When two or more classes are derived from a single base class, then Inheritance is called the hierarchical inheritance. Syntax class derived_class_name1:visibility base_class_name { . . }; class derived_class_name2:visibility base_class_name { . . };
  19. 19. MULTILEVEL INHERITANCE In multilevel inheritance the derived class can also be derived by an another class, which in turn can further be inherited by another and so on. Syntax class derived_class_name1:visibility base_class_name { . . }; class derived_class_name2:visibility derived_class_name1 { . . };
  20. 20. PROGRAM USING SINGLE INHERITACE IN C++ #include<iostream.h> #include<conio.h> class A { public: void add() { int a=5,b=4,s; s=a+b; } }; class B: public A { public: void show() { cout<<”sum is:”<<s; } //class B publically inherit class A//
  21. 21. }; void main() { B obj; obj.add();; getch(); } Output: sum is:9
  22. 22. MERITS OF INHERITANCE Increase reuse and software quality programmers reuse the base class instead of writing new class using well tested base class helps reduce bugs in applications that use them Reduce object code size Enhance extensibility and comprehensibility helps support more flexible and extensible architectures often useful for modeling and classifying hierarchically- related domains
  23. 23. DEMERITS OF INHERITANCE May create deep hierarchies that are difficult to understand. May decrease performance slightly. Subclass become dependent on parent class implementation. Complexity for very small programs.
  24. 24. INHERITANCE IN C# C# supports two types of Inheritance mechanisms 1) Implementation Inheritance When a class (type) is derived from another class(type) such that it inherits all the members of the base type it is Implementation Inheritance 2) Interface Inheritance When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance.
  25. 25. PROGRAM USING SINGLE INHERITANCE IN C# using System; class A { public int x = 10; public int y = 20; } class B : A { public int z = 30; public void Sum() { int sum = x+y+z; Console.WriteLine(“sum is:{0}”,sum); } }
  26. 26. class add { public static void Main() { Derived d1 = new Derived(); d1.Sum(); } } Output: sum is: 60
  27. 27. MERITS OF INHERITANCE IN C# Through inheritance you can very easily convert small system into large systems You can have lots of classes with the same root but have different implementations of classes. Code reusability through inheritance increased. Code are easy to manage and divided into parent and child classes.
  28. 28. DEMERITS OF INHERITANCE IN C# We can't change the implementation inherited from super classes at runtime (obviously because inheritance is defined at compile time). Inheritance exposes a subclass to details of its parent's class implementation, that's whey it's often said that inheritance breaks encapsulation (in a sense that you really need to focus on interfaces only not implementation, so reusing by sub classing is not always preferred). The tight coupling provided by inheritance makes the implementation of a subclass very bound up with the implementation of a super class that any change in the parent implementation will force the sub class to change. Excessive reusing by sub-classing can make the inheritance stack very deep and very confusing too.
  29. 29. POLYMORPHISM Polymorphism is a key to the power of OOP. It is the concept that supports the capability of data to be processed in more than one form. For example, an operation may exhibit different behaviour in different instances. The behaviour depends upon the types of data used in the operation. Let us consider the operation of addition.For two numbers, the operation will generate a sum. If the operands are strings then the operation would produce a third string by concatenation. Polymorphism is of two types: Compile time Run time
  30. 30. COMPILE TIME POLYMORPHISM Compile time polymorphism is method and operators overloading. It is also called early binding. Method with same name but with different arguments is called method overloading. In method overloading, method performs the different task at the different input parameters. EXAMPLE OF METHOD OVERLOADING class A1 { void hello() { Console.WriteLine(“WELCOME”); } void hello(string s) { Console.WriteLine(“WELCOME {0}”,s); } }
  31. 31. RUN TIME POLYMORPHISM Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass. When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.Method overloading has nothing to do with inheritance or virtual methods.
  32. 32. EXAMPLE OF METHOD OVERRIDING Class parent { virtual void hello() { Console.WriteLine(“Hello from Parent”); } } class child:parent { override void hello() { Console.WriteLine(“Hello from Child”); } } static void main() { parent objParent = new child(); objParent.hello(); } Output Hello from Child.
  33. 33. DIFFRENCE BETWEEN INHERITANCE AND POLYMORPHISM Inheritance is the object oriented feature by which the functionality and features of one class are made available to another class without having to copy paste the code. Polymorphism is the object oriented feature by which multiple methods of a class can coexist with minor differences. Ex: method overloading.
  34. 34. SIMILARITIES BETWEEN C++ & C# Both supports pointers. Capable of defining pre-processor. Both supports structure. Both supports goto statement.
  35. 35. POINTER IN C++ A pointer is a variable that is used to store a memory address. The address is the location of the variable in the memory. Pointers help in allocating memory dynamically. The general form of declaring pointer is:- type *variable_name; type is the base type of the pointer and variable_name is the name of the variable of the pointer. Pointer Operators There are two important pointer operators such as ‘*’ and '&' .The ‘&’ is a unary operator. The unary operator returns the address of the memory where a variable is located. The ‘*’ operator is called the indirection operator. It returns the contents of the memory location pointed to. The indirection operator is also called deference operator.
  36. 36. PROGRAM USING POINTER #include<iostream.h> #include<conio.h> void main( ) { int *x; int c=100; x=&c; cout << &quot; The address of the memory location of x : &quot; << x <<endl; cout << &quot; The contents of the pointer x : &quot; << *x << endl; getch( ); } Output: The address of the memory location of x: 01212FF78 The contents of the pointer x: 100
  37. 37. POINTER IN C# Pointers are special kind of variables which hold addresses other variables.To create a pointer we use the following declaration: type* variable_name; As a type may be used each type that is not a reference-type and does not contain reference-type field. It can be only: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool any enum-type. Pointer are only be used in unsafe context.
  38. 38. PROGRAM USING POINTER using System; class A { public unsafe static void main() { int i=10; int *p; p=&x; console.WriteLine(“address is”+ p); console.WriteLine(“value is”+ *p); } } Output Address is 0012HM95 value is 10';
  39. 39. PREPROCESSOR IN C ++ The pre-processor is a utility program, which processes special instructions that can be or are written in a C++ program. These instructions can be include a library or some special instructions to the compiler about some certain terms used in the program. Pre-processor directives start with ‘#’ character.Pre-processor directives starts with # character. C++ supports the following preprocessor directives: #include #define #if #elif #else #endif #ifndef #ifdef #undef
  40. 40. PREPROCESSOR IN C# A preprocessor directive must be the only instruction on a line. Preprocessing directives are lines in your program that start with '#'. Whitespace is allowed before and after the '#'. The '#' is followed by an identifier that is the directive name. For example, '#define' is the directive . The C# language's preprocessor directives are as follows: #if #else #elif #endif #define #undef #error #line
  41. 41. STRUCTURE Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. Declaring a Structure: The structure is declared by using the keyword struct followed by structure name, also called a tag. Then the structure members (variables) are defined with their type and variable names inside the open and close braces { and } . Finally, the closed braces end with a semicolon denoted as ; following the statement. Struct student { char name[20]; int marks; int total; }; Keyword Structure name Structure member
  42. 42. Goto statement The goto statement translates the program control directly to a labeled statement. It takes following form: goto identifier; goto case const-exp.; goto default;
  43. 43. DIFFERENCES BETWEEN C++ AND C# Most of the C# features are similar to those of C++. However, C# provides various additional features that make it a more type –safe and web enabled language. Various points of different between C# and C++ are : C# does not use semicolon at the end of class definition. C# does not contain any default constructor. Objects in C# can only be created using new keywords. C# does not support multiple inheritance C# does not support the header file,# include. Continue..
  44. 44. Continue... C++ supports operator overloading but C# does not. C++ is not completely object oriented based but C# is completely object oriented. C# have garbage collection capability but it is absent in C++ . C++ is statically typed whereas C# is dynamic in nature. C# is high level programming language while C++ is a middle level programming language.
  45. 45. ADVANTAGES OF C++ Better performance Portability Multiple inheritance Support for global variables, function and constants
  46. 46. DISADVANTAGE OF C++ Complex in very large high level program. C++ can't support garbage collection. C++ is not secure because it have pointer, friend function and global variable. When C++ used for web applications complex and difficult to debug .
  47. 47. ADVANTAGES OF C# Garbage collection Huge .NET framework library Supports constructor chaining No need for header files and #include Access to class members/ function is done only by the dot(. )
  48. 48. DISADVANTAGE OF C# C# is less flexible than C++. C# depends greatly on .NET framework, anything that is not found in the .NET framework will be difficult to implement. All though portability is what with C# was developed, C# programs are largely for Microsoft Windows Environment which although happens to be the most widely used Operating system over the globe, but with Open Source technologies, especially platforms like Linux catching up, C# still has to long way to go. This is one major shortcoming of C# . Although C# programs can be written as little as a text editor and compiled using command line, it still needs the .NET framework to be present on the system, which cripples down the portability in the development of C# programs by a large amount, and makes it more Microsoft Windows dependent.
  49. 49. CONCLUSION While there are a number of differences between C++ and C#, the syntax of C# is not very different from C++ and the transition to the new language is more easy with .NET framework. Also .NET's cross language interoperability can give us the ability to use C++ and C# together Finally, if we want to use a strong programming language like C++ with the simplicity of VB than we can use C# powered with CLR.
  50. 50. REFERENCES