Anda di halaman 1dari 15

OBJECT ORIENTED PROGRAMMING SYSTEM (OOPS)

STATIC DATA MEMBERS


It has certain characteristics
It is initialized to zero when the first object of its class is created Only one copy is created for entire class, shared by all the objects of that class Visible only within the class Life time is the entire program

Default arguments
Prototype default values
Default values are specified when the function is declared We must add default from right to left

Float amout( float principle, int period, float rate=0.15); Value = amount(5000, 7); Value = amount(500, 5.0, 0.12);

Example for Default arguments

# include <iostream.h> # include <conio.h> void main() { float amount; float value(float p, int n, float r =0.15); void printline(char ch=*, int len=40); printline(); amount = value(5000.00, 5); cout << \n << amount << \n; printline(=); } float value(float p, int n, float r) { int year = 1; float sum = p; cout<<year<<sum<<r; }

void printline(char ch, int len) { for(int i=0; i<=len;i++) Cout<<ch; }

Friend Function
Non- member function cannot have an access to the private data of a class

C++ allows common functions to be made friendly with both the classes

The function declaration should be proceeded by the keyword friend

Friend function

# include <iostream.h> # include <conio.h>


class sample { int a; int b; public: void setvalue () { a=20; b=25; } friend float mean(sample s); } float mean(sample s) { return float(s.a +s.b)/2.0; }

int main() { sample x; x.setvalue(); cout << Mean value = << mean(x) << \n; return 0; }

Function overloading
Use the same function name to create functions that perform a variety of different tasks known as function overloading Function overloading rules:
Function name should be same Number of arguments should be differ otherwise arguments data type should be differ Example
int Add(int a, int b); int Add(int a, int b, int c); int Add(double a, double b))

# include <iostream.h> # include <conio.h> int volume(int); double volume(double, int); long volume(long, int, int); int main( ) { cout<< volume(10) << \n; cout << volume(2.5, 8) << \n; cout << voume(100l, 75, 15); return 0; } int volume (int s) { return(s*s*s); }

double volume(double r, int h) { return(3.14*r*r*h); }

long volume(long l, int b, int h) { return(l*b*h); }

Static member function


A static function can have access to only other static members declared in the class

A static member function can be called using the class name


Class name :: function name

Static void showcount (void) { cout << count: << count << \n; }

Static member function

Class test { int code; static int count; Public: Void setcode(void) { code = ++count; } Void showcode(void) { Cout << Object number: << code <<\n; } Static void showcount (void) { cout << count: << count << \n; }
};

int main() { test t1,t2; t1.setcode(); t2.setcode(); test :: showcount(); test t3; t3.setcode(); test :: showcount(); t1.setcode();

t2.setcode(); t3.setcode();
}

Const member functions


If a member function does not alter any data in the class, then we declare it as a const member function

Void mul(int, int) const; double display() const;

Compiler will generate an error msg if such functions try to alter the data

Anda mungkin juga menyukai