Вы находитесь на странице: 1из 4

functions in C types - pre define & user define user define - 4 * function with * function with * function with

* function with syn returntype funname(argument/s) { statement/s; return val; - if return type given } Adv of functions - functions r used for implement the modular appoach. * Simplicity * Reusablility * Performence Improve * Error Handling & debugging easy. // Type 1 - No pass & no return #include<stdio.h> void disp() { printf("I m inside disp\n"); } void main() { clrscr(); disp(); getch(); } // Type 2 - No pass & return #include<stdio.h> int getVal() { int x; printf("Plz enter any Number : "); scanf("%d",&x); return x; } void main() { int a,b,c; clrscr(); a = getVal(); b = getVal(); c = a + b; printf("Sum is = %d",c); getch(); } // Type 3 - pass & no return #include<stdio.h> void sum(int x,int y) Types no pass argument & no return value. - no pass no return no pass argument & return value. - no pass & return pass argument & no return value. - pass & no return pass argument & return value. - pass & return

{ int c = x + y; printf("Sum is = %d\n",c); } void main() { int a,b; clrscr(); printf("Plz enter 2 Numbers : "); scanf("%d %d",&a,&b); sum(a,b); getch(); } // Type 4 - pass & return #include<stdio.h> int sum(int x,int y) { return = x + y; } void main() { int a,b,c; clrscr(); printf("Plz enter 2 Numbers : "); scanf("%d %d",&a,&b); c = sum(a,b); printf("Sum is = %d\n",c); getch(); } methods in java = C & C++ - 4 Types + 2 Types extra Concrete methods Abstract methods * with body * without body. * can call without overriding* 1st override then we can call it void disp(){S.o.p("jaja");} void disp(); abstract method - just prototype for method or like function declarations in C & C++ or like pure virtual functions in C++. prototype in C or Function declaration in C #include<stdio.h> void disp(); // prototype for disp - abstract method void main() { clrscr(); disp(); getch(); } void disp() // def. for disp - override { printf("I m inside disp\n"); } or

#include<stdio.h> void disp() // prototype + def. - Concrete method { printf("I m inside disp\n"); } void main() { clrscr(); disp(); getch(); } classes Type - 2 Types Concrete class * only Concrete methods. * can create instance & inherit also. Abstract class * Concrete + abstract methods. * can't create instance inherit only.

Abstract class - if one class has one or more abstract methods then that is called an Abstract class, we can't create instance of an abstract class it is only for inherit purpose, used for create/design the Generic methods. Adv - Abstract classes r used for memory management, memory for methods provided by the abstract class, used for create/design the Generic methods. Q- we can can create an abstract class without any abstract method? Ans- yes, can create, just declare as abstract. but can't create the direct instance. conditions - all abstract methods of an abstract class must be override in child class else child class is also a kind of abstract class. future - Abstract classes not in used, bcos using classes java support only single inheritance, then one class can extends only with one abstract class or one concrete class. now inherit with concrete class & the place of abstract class used to concept of interfaces. interface - it is a kind of pure abstract class. // to create simple abstract class abstract class Calc { void sum(int a,int b) // concrete method { int c = a + b; System.out.println("Sum is = "+c); } abstract void sub(int a,int b); // abstract method } // class inherit with abstract class class MyCalc extends Calc

{ void sub(int a,int b) // method of Calc class { int c = a - b; System.out.println("Sub is = "+c); } public static void main(String aa[]) { MyCalc o = new MyCalc(); o.sum(100,20); o.sub(100,20); } }

Вам также может понравиться