Showing posts with label Programming in C Plus Plus. Show all posts
Showing posts with label Programming in C Plus Plus. Show all posts
Constructor and De-constructor

Constructor and De-constructor

Constructor and De-constructor- 


Constructor-
Constructor เคเค• เคช्เคฐเค•ाเคฐ เค•े special member function เคนै เคœो เค•ि class เค•े object เค•ो initalize เค•เคฐเคจे เค•े เค•ाเคฎ เค†เคคा เคนै เคฏเคน special เค‡เคธเคฒिเค เคนै เค•्เคฏोंเค•ि เค‡เคธเค•ा name เคตเคนी เคนोเคคा เคนै เคœो เค•ि class เค•ा name เคนोเค—ा।
(1) Constructor เค•ो public part เคฎें declare เค•िเคฏा เคœाเคคा เคนै।
(2) เค‡เคจเค•ा เค•ोเคˆ return type เคจเคนी เคนोเคคा เคนै।
(3) เค‡เคจเค•ो inherit เคจเคนी เค•िเคฏा เคœा เคธเค•เคคा เคนै।

Types of Constructor-
Constructor เคฆो เคช्เคฐเค•ाเคฐ เค•े เคนोเคคे เคนै।
(I) Default constructor
(II) Parameterized constructor

(I) Default constructor- เค‡เคจเค•ा เค‰เคชเคฏोเค— object เค•े เคธाเคชेเค•्เคท scope resolution operator (::) เค•े เคฆ्เคตाเคฐा เค•िเคฏा เคœाเคคा เคนै।
เคฏเคน constructor เค…เคชเคจे เคชाเคธ เค•ोเคˆ argument เคจเคนी เคฐเค–เคคे เคนैं।
Example-
Class fruit
{
int x,y;
Public:
fruit();
-------------
-------------
};
fruit :: fruit()
{
x=5;
y=3;
----------
----------
}


(II) Parametrized constructor- เค‡เคธเคฎें constructor argument เคฒे เคธเค•เคคा เคนै। Argument เค•ो value เคช्เคฐเคฆाเคจ เค•เคฐเคจे เค•े เคฒिเค constructor เค•ो call เค•िเคฏा เคœाเคคा เคนै।
Example-
Class fruit
{
int x,y;
Public:
fruit(int a, int b);
-------------
-------------
};
fruit :: fruit(int a, int b)
{
x=a;
y=b;
----------
----------
}





De-Constructor- 
Constructor เคฆ्เคตाเคฐा เคฌเคจाเคฏे เค—เคฏे objects เค•ो destroy เค•เคฐเคจे เค•े เคฒिเค
De-constructor เค•ा use เคนोเคคा เคนै।
De-Constructor เคญी เคเค• member function เคนै, เคœिเคธเค•ा name class name เคนी เคนोเคคा เคนै เคคเคฅा constructor เค•เคญी เคญी เค•िเคธी เคญी เคช्เคฐเค•ाเคฐ เค•ा argument เคจเคนी เคฒेเคคा เคนै เคจ เคนी เค•ोเคˆ value return เค•เคฐเคคा เคนै।
◆De-Constructor เค•े name เค•े เค†เค—े tilde(~) เค•ा เคช्เคฐเคฏोเค— เค•िเคฏा เคœाเคคा เคนै।
Example-
Class fruit
{
int x, y;
Public:
fruit()
~ fruit();
Cout<<"destroyed";
}
fruit :: fruit(int a, int b)
{
x=a;
y=b;
-------------
-------------
}





Inline Function in C++

Inline Function in C++

Inline Function-



C++ เคฎें เคœเคฌ เคเค• function เค•ो เค•เคˆ เคฌाเคฐ call เค•िเคฏा เคœाเคฏे เคคो เค•เคˆ เคธाเคฐा memory space เคคเคฅा time เคฒเค—เคคा เคนै เคชเคฐंเคคु function เค›ोเคŸा เคนो เคคो execution time เค•ो เค•เคฎ เค•เคฐเคจे เค•ा เคคเคฐीเค•ा เคนै macros เค•ा เค‡เคธ्เคคेเคฎाเคฒ।

            C เคฎें macros เค•ा เค‡เคธ्เคคेเคฎाเคฒ เค•िเคฏा เคœाเคคा เคฅा เคชเคฐंเคคु เค‡เคธเค•ा เคฏเคน disadvantage เคนै เค•ि macros real function เคจเคนी เคนोเคคे เคนै เคคเคฅा compile เค•เคฐเคคे เคธเคฎเคฏ เค‡เคจเคฎे error check เคจเคนी เคนोเคคी เคนै।
            Inline Function C++ เคฎें function เค•ो call เค•เคฐเคจे เค•ी process เค•ो เค†เคธाเคจ เค•เคฐเคคे เคนुเค เคŠเคชเคฐ เคฆी เค—เคฏी เคธเคฎเคธ्เคฏा เค•े เคธเคฎाเคงाเคจ เค•ो เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै, เค•िเคธी เคญी function เค•ो inline เคฌเคจाเคจे เค•े เคฒिเค function เค•े เค†เค—े invoke เคถเคฌ्เคฆ เค•ो เคฒिเค– เคฆिเคฏा เคœाเคคा เคนै।

Inline function เค•ो เคœिเคธ line เคฎें เคฒिเค–ा เคœाเคคा เคนै เคตเคน invoke เคนुเค† เคนो เคตเคนी เคชเคฐ expand เค•เคฐเค•े define เค•िเคฏा เคœा เคธเค•เคคा เคนै।

Inline Function เค•ी calling direct เคนै เคคเคฅा inline keyword compiler เค•ो command เคจเคนी requst เคญेเคœเคคा เคนै เค•ि เค‰เคธे เค•ाเคฐ्เคฏ เค•เคฐเคจे เคฆिเคฏा เคœाเคฏे เคชเคฐंเคคु เค•ुเค› situation เคชเคฐ inline expansion เค•ाเคฎ เคจเคนी เค•เคฐเคคा เคนै เคœैเคธे เค•ि-
(i) เค…เค—เคฐ inline function Recursive เคนो।
(ii) function เค•े เค…ंเคฆเคฐ static เคฎाเคจ เคนो।
(iii) เค…เค—เคฐ function เค•े เค…ंเคฆเคฐ value return เคนो เคฐเคนी เคนो เคคเคฅा เค‰เคธเคจे switch เคฏा goto เค•ा เคช्เคฐเคฏोเค— เค•िเคฏा เคนो।




Example- 
#include<iostream.h>
Inline float add (float a, float b)
{
return(a+b);
}
int main()
{
float a=5.4
float b=7.4
Cout<<add(a,b)
return 0;
}



เคœเคพเคจเฅ‡ เคธเฅ€++ เคฎเฅ‡เค‚ เคซเฅเคฐเฅ‡เค‚เคก เคซเค‚เค•เฅเคถเคจ เค•เฅเคฏเคพ เคนเฅ‹เคคเคพ เคนเฅˆ เคคเคฅเคพ เค‡เคธเค•เฅ‡ เค•เฅเคฏเคพ เค•เฅเคฏเคพ เคฒเคพเคญ เคนเฅ‹เคคเฅ‡ เคนเฅˆเฅค

เคœाเคจे เคธी++ เคฎें เคซ्เคฐेंเคก เคซंเค•्เคถเคจ เค•्เคฏा เคนोเคคा เคนै เคคเคฅा เค‡เคธเค•े เค•्เคฏा เค•्เคฏा เคฒाเคญ เคนोเคคे เคนै।

Friend Function -


C++ เคฎें friend function เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœा เคธเค•เคคा เคนै, Friend function เค•ी เคจिเคฎ्เคจเคฒिเค–िเคค characterstics เคนोเคคी เคนै।



★ friend function เค•ो class เค•े object เค•े เคœเคฐिเคฏे access เคจเคนी เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★ เค‡เคธเค•ो เค•िเคธी เคญी normal function เค•ी เคคเคฐเคน เคฌिเคจा object เค•े invoke เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★ เค•िเคธी function เค•ो เค•िเคคเคจी เคญी class เคฎें friend define เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★ friend function เค•ो class เค•े public เคฏा private เค•िเคธी เคญी part เคฎें define เค•िเคฏा เคœा เคธเค•เคคा เคนै।
★friend function เค•ो เค•िเคคเคจी เคญी class เคฎें friend define เค•िเคฏा เคœा เคธเค•เคคा เคนै।

■ เค•िเคธी เคญी function เค•ो friend function เคฌเคจाเคจे เค•े เคฒिเค function เค•े เค†เค—े Friend เคฒिเค–ा เคœाเคคा เคนै।

Example- Friend void display(N);
เคœเคนा N เค•ोเคˆ object เคนोเค—ा।




Pure Virtual Function

Pure Virtual Function

Pure Virtual Function -

เค…เค—เคฐ เคนเคฎ base class เคฎें เค•िเคธी function เค•ो define เค•เคฐे เคคเคฅा เค‰เคธเค•ी เคชुเคจः definition derived class เคฎें เคฆी เคœाเคฏे เคคो base class เค•ा เคตเคน function do-nothing เคช्เคฐเค•ाเคฐ เค•ा เคนै, เค‡เคธ เคช्เคฐเค•ाเคฐ เค•े function เค•ो Pure Virtual Function เค•เคนा เคœाเคคा เคนै เคคเคฅा เคเคธी classes เคœिเคจเคฎें Pure Virtual Function เคฌเคจा เคนो Abstract Class เค•เคนเคฒाเคคी เคนै।

//virtual void display()=0;


Example-
class fruit      //base class
{
//virtual void display()=0;
}
class apple : public fruit  // abstract class
{
void display()
{
Cout<<"this is a Pure Virtual Function";
}
}



เคตเคฐเฅเคšเฅเค…เคฒ เคซเค‚เค•เฅเคถเคจ เค•เฅเคฏเคพ เคนเฅ‹เคคเคพ เคนเฅˆ เคเคตเค‚ เค‡เคธเค•เฅ‡ เค•เฅเคฏเคพ เค‰เคชเคฏเฅ‹เค— เคนเฅ‹เคคเฅ‡ เคนเฅˆเฅค

เคตเคฐ्เคšुเค…เคฒ เคซंเค•्เคถเคจ เค•्เคฏा เคนोเคคा เคนै เคเคตं เค‡เคธเค•े เค•्เคฏा เค‰เคชเคฏोเค— เคนोเคคे เคนै।

Virtual Function-


Virtual Function เค•ा use run time polymorphism เค•ो เคช्เคฐाเคช्เคค เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै।
เค…เค—เคฐ เคนเคฎाเคฐे เคชाเคธ เคฆो class เคนो เคเค• base class เคคเคฅा เคฆूเคธเคฐी derived class เคนो เค”เคฐ เคนเคฎ pointer เค•ा เค‡เคธ्เคคेเคฎाเคฒ เค•เคฐเค•े derived class เค•े object เค•ो access เค•เคฐเคจा เคšाเคนเคคे เคนो เคคो เคเคธा เคจเคนी เคนो เคชाเคคा เคนै เค•्เคฏोंเค•ि base class เค•ा pointer เคนเคฎेเคถा base class เค•े function เค•ो เคนी execute เค•เคฐेเค—ा।
เค‡เคธ problem เค•ो solve เค•เคฐเคจे เค•े เคฒिเค virtual function use เค•िเคฏा เคœाเคคा เคนै।

ukpolytechnicnotes.blogspot.com

            Base class เค•े function เค•ो virtual เคฌเคจाเคฏा เคœाเคคा เคนै เคœिเคธเค•े เคฒिเค base class เค•े เค†เค—े Virtual เคถเคฌ्เคฆ เคฒिเค– เคฆिเคฏा เคœाเคคा เคนै เคœเคฌ เค•ोเคˆ function virtual เคฌเคจा เคฆिเคฏा เคœाเคคा เคนै เคคो C++ base pointer เคฆ्เคตाเคฐा point เค•िเคฏे เค—เค object เคธे เคฏเคน เคจिเคฐ्เคงाเคฐिเคค เค•เคฐเคคी เคนै เค•ि เค•िเคธ function เค•ो เคšเคฒाเคจा เคนै।

Example-
class base
{
void C()
{
Cout<<"C is here";
}
Virtual void C++()       //virtual Function
{
Cout<<"C++ is here";
}
}
class derived : Public base
{
void C()
{
Cout<<"C is there";
}
void C++()
{
Cout<<"C++ is there";
}
}
void main()
{
base.b1;       //object
derived d1;    //object
base*pptr;      //pointer
bptr=&b1;
bptr->C();      // C is here
bptr->C++();   C++ is here
bptr=&d1;
bptr->C();      // C is here
bptr->C++();   //C++ is here
//Output virtual Function
}

derived class เค•े object C function เค•ो class เค•เคฐเคจे เคชเคฐ base class เค•ा เคนी output เค†เคฏेเค—ा, เค•्เคฏोเค•ि base pointer เคนเคฎेเคถा base class เค•े function เค•ो เคนी เคฒाเคคा เคนै เคชเคฐंเคคु เคฏเคฆि base class เค•ा function virtual เคนो เคœैเคธा เค•ि C++ name เค•ा function เคนै เคคो derived class เค•ा function call เคนो เคœाเคคा เคนै।

ukpolytechnicnotes.blogspot.com





Polymorphism

Polymorphism

Polymorphism-

Poly means many (เค•เคˆ เคธाเคฐे) เคนोเคคा เคนै, เคฏเคน OOPS (object-oriented programmingเค•ा เคเค• concept เคนै เคœिเคธเคฎें เค•िเคธी function เคฏा operator เค•ो เค•เคˆ เคคเคฐीเค•ों เคธे เคเค• เคนी name เค•े เคธाเคฅ access เค•िเคฏा เคœा เคธเค•เคคा เคนै ।
Polymorphism are two types.


Compile time Polymorphism-
Program เค•ो compile เค•เคฐเคคे time เคเค• เคนी name เค•े function เค•ी เค…เคฒเค— definition compile เค•เคฐ เคฆी เคœाเคคी เคนै।
Compile time polymorphism เคฆो เคช्เคฐเค•ाเคฐ เค•ा เคนोเคคा เคนै।

(i) Function Overloading- เคเค• เคนी name เค•े function เคชเคฐंเคคु เค…เคฒเค— เค…เคฒเค— parameter use เคนो เคคो function overloading เค•เคนเคฒाเคคी เคนै।

Example- 
void area (int a, int b)
void area (int c)
void area (int d, int e, int f)
int area (int a)
{
return (a*a);
}
int area (int b, int c)
{
return (b*c);
}
Cout<<area(5);      
// output=25(area of square)
Cout<<area(5,10);
// output=50(area of rectangle)

(ii) Operator Overloading- เคเค• single operator เค•ो เคฆो เคฏा เคฆो เคธे เค…เคงिเค• เค•ाเคฐ्เคฏो เค•े เคฒिเค เคช्เคฐเคฏुเค•्เคค เค•िเคฏा เคœाเคฏे เคคो operator overloading เค•เคนเคฒाเคคी เคนै।
Example-
5+2=7
เค‡เคธเคฎें '+' operator เคฆ्เคตाเคฐा addition เค•िเคฏा เค—เคฏा เคนै เคเค• new (diffrent) output เคช्เคฐाเคช्เคค เคนुเค†।

ganesh+gururani=ganeshgururani
เค‡เคธ example เคฎें เค‰เคธी '+' operator เค•ो เคฆो string เค•ो catenate(add) เค•เคฐเคจे เค•े เคฒिเค เคช्เคฐเคฏोเค— เค•िเคฏा เค—เคฏा เคนै เค‡เคธเคธे เคœो output เคช्เคฐाเคช्เคค เคนुเค† เคนै เคตเคน input เคธे เคฎिเคฒเคคा เคœुเคฒเคคा (เคธเคฎाเคจ) เคนै।

Run Time Polymorphism- Program เค•े run เคนोเคจे เคชเคฐ เคเค• เคนी name เค•े เคฆो เคฏा เคฆो เคธे เค…เคงिเค• function เคฎें เคธे เค•िเคธे execute เคนोเคจा เคนै เคฏเคน decide เค•िเคฏा เคœाเคฏे เคคो เคตเคน run time เคนै।
★Run time polymorphism เค•ो virtual function เค•े เคœเคฐिเคฏे เคช्เคฐाเคช्เคค เค•िเคฏा เคœा เคธเค•เคคा เคนै।




Abstract Class

Abstract Class

Abstract Class-


Abstract class เคเค• เคเคธी class เคนै เคœिเคธเค•ा เค‡เคธ्เคคेเคฎाเคฒ object เคฌเคจाเคจे เค•े เคฒिเค เคจเคนी เค•िเคฏा เคœाเคคा เคนो।
★Abstract class เค•ी designing เคฎाเคค्เคฐ เค‰เคธे base class เค•ी เคคเคฐเคน act เค•เคฐเคจे เค•े เคฒिเค use เค•ी เคœाเคคी เคนै।
★ เคเค• เค•्เคฒाเคธ abstract class เคคเคฌ เค•เคนเคฒाเคคी เคนै เคœเคฌ เค‰เคธเคฎे เค•เคฎ เคธे เค•เคฎ เคเค• pure virtual function เคนो।



Data abstraction & encapsulation

Data abstraction & encapsulation

Abstraction & encapsulation-

Data  เคคเคฅा function เค•ो เคเค• single unit เคฎें wrap เค•เคฐเคจा encapsulation เค•เคนเคฒाเคคा เคนै เคคเคฅा เคตเคน single unit เคœिเคธเคธे data wrap เคนुเค† เคนै class เค•เคนเคฒाเคคी เคนै, เคเค• class เค•े เค…ंเคฆเคฐ data outside world เคธे access เคจเคนी เค•िเคฏा เคœा เคธเค•เคคा, เค…เค—เคฐ เคœเคฐुเคฐी features เค•ो show เค•เคฐ background detail เค•ो hide เค•เคฐ เคฆिเคฏा เคœाเคฏे เคคो เคฏเคน เคคเคฐीเค•ा data abstraction เค•เคนเคฒाเคคा เคนै।



Inheritance เค•เคฟเคคเคจเฅ‡ เคชเฅเคฐเค•เคพเคฐ เค•เคพ เคนเฅ‹เคคเคพ เคนเฅˆ

Inheritance เค•िเคคเคจे เคช्เคฐเค•ाเคฐ เค•ा เคนोเคคा เคนै

Types of inheritance- 
inheritance are following five types.

(1)
Single inheritance- เคฏเคนाँ เคเค• base class เคคเคฅा เคเค• derived class เคนोเคคी เคนै।
(class B : Public A)

(2) Multiple inheritance- เคฏเคนाँ เคชเคฐ เคฆो เคฏा เคฆो เคธे เคœ्เคฏाเคฆा base class เคคเคฅा เคเค• derived class เคนोเคคी เคนै।
class C : Public A, Public B

(3) Multilevel inheritance- เคฏเคนाँ เคชเคฐ เคเค• base class เคเค• intermediate base class เคคเคฅा เคเค• derived class เคนोเคคी เคนै।
class B : Public A
class C : Public B

(4) Hierachical inheritance- เค‡เคธเคฎें เคเค• เคธे เคœ्เคฏाเคฆा derived class เคเค• single base class เคธे inherit เคนोเคคी เคนै।

(5) Hybrid inheritance- เค‡เคธเคฎें เค•िเคธी เคญी เคฆो เคฏा เคฆो เคธे เค…เคงिเค• inheritance เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœाเคคा เคนै।



C++ เคฎเฅ‡เค‚ inheritance เค•เฅเคฏเคพ เคนเฅ‹เคคเคพ เคนเฅˆ?

C++ เคฎें inheritance เค•्เคฏा เคนोเคคा เคนै?

Inheritance-
C++ เคฎें เค•िเคธी function เคฏा data type เค•ो reuse เค•िเคฏा เคœा เคธเค•เคคा เคนै, เคœिเคธเคธे เค‰เคธे เคฆुเคฌाเคฐा create เค•เคฐเคจे เค•ी เค†เคตเคถ्เคฏเค•เคคा เคจा เคชเฅœे।
C++

C++ เคฎें เคเค• class เคฌเคจाเคจे เคชเคฐ เค…เค—เคฐ เค‰เคธ class เค•े data type เคฏा function เค•ो access เค•เคฐเคจा เคนो เคคो เคเค• new class เค•ा เคจिเคฐ्เคฎाเคฃ เค•เคฐเคจा เคนोเคคा เคนै।
★ Old class เคธे new class เค•ो derive เค•เคฐเคจा inheritance เค•เคนเคฒाเคคा เคนै।
●Old class เค•ो base class เคฏा super class เค•เคนเคคे เคนै เคคเคฅा derived class เคฏा sub class properties เค•ो inherit เค•เคฐเคคी เคนै, 
● Inheritance เค•ो use เค•เคฐเคจे เค•े เคฒिเค colon
(:) operator เค•ा เค‡เคธ्เคคेเคฎाเคฒ เคนोเคคा เคนै।
Class B : A
เค•िเคธी class เค•ो inherit เค•เคฐเคจे เค•े เคฒिเค Public เคฏा Private mode เค•ा เค‡เคธ्เคคेเคฎाเคฒ เคญी เค•िเคฏा เคœा เคธเค•เคคा เคนै, Default mode Private เคนोเคคा เคนै।

C++


Example of inheritance:-
class A
{
int a,b,c,d;
void sum()
{
Court<<"enter the number";
Cin>>a>>b;
C=a+b;
Court<<c;
}
class B : Public A
{
void subtract()
{
d=c-a;
Cout<<a;
}
main()
{
A a1;
B b1;
a1.sum();
b1.subtract();
}
}
}


Class & object in c++

Class & object in c++

Class & Object:- เค•िเคธी เคญी program เคฎें เค•เคˆ function เคคเคฅा เคตिเคญिเคจ्เคจ เคช्เคฐเค•ाเคฐ เค•े data type เคฎौเคœूเคฆ เคนोเคคे เคนै,
Similar data type เคคเคฅा member function เค•ा group class เค•เคนเคฒाเคคी เคนै। เคเค• class เคธเคฎाเคจ เคช्เคฐเค•ाเคฐ เค•े object เค•ा เคธเคฎूเคน เคนै।

Object เค•िเคธी เคญी class เค•े variable เค•ी เคคเคฐเคน เค•ाเคฎ เค•เคฐเคคे เคนै เคคเคฅा เคเค• class เค•े เคฒिเค เค•เคˆ เคธाเคฐे object defined เค•िเค เคœा เคธเค•เคคे เคนै।
Class เค•ा เคเค• name เคนोเคคा เคนै เคœो user dependent เคนोเคคा เคนै।

Class define เค•เคฐเคจे เค•े เคฒिเค:-
Class<class name>
{
---------//member function, data type
---------
}

Class เคฎें object define เค•เคฐเคจे เค•े เคฒिเค:-
<class name> <object name>;

Object เค•ा name เคญी user dependent เคนोเคคा เคนै।
เค•िเคธी เคญी class เค•े function เค•ो access เค•เคฐเคจे เค•े เคฒिเค object เค•ा เค‡เคธ्เคคेเคฎाเคฒ เคนोเคคा เคนै।

<object name>.<function name>;

Example class & object:-

#include<iostream.h>
class add            //define class
{
int a, b, c;
void sum()
{
Cout<<"enter the no.";
Cin>>a>>b;
c=a+b;
Cout<<c;
}
}
void main()
{
add a1;         // object defind in class
a1.sum();
}