write a program to develop string class function using member function .
Explanation
सबसे पहले class string को declare किया जाता है | इस class मे एक variable length , first , last और full_name को declare किया जाता है | इसका access mode private होता है अतः इसे class string मे ही use किया जा ससकता है | class string मे तीन function के header को declare किया जाता है |
len() : इसका access mode public है | इसमें string के length को find किया जाता है | इस function से return हुई value को private mode length मे assign किया जाता है |
compare() : इसमें compare operation को perform किया जाता है |
concentrate () : इसमें दो string को merge किया जाता है |
इसके बाद scope resolution operator से class स्क्क्सोपे के बहार इन सभी function को define किया जाता है |
string :: length () मे ,
सबसे पहले function मे यूजर द्वारा input की गयी string first को pass किया जाता है | length को find करने के लिए loop को चलाया जाता है |
loop मे control variable ‘i’ को ‘0’ से initial किया जाता है | ये loop तब तक चलाया जाता है जब तक string की value null pointer नहीं होती है |
loop की body मे , length के value को update किया जाता है | इसके बाद control variable की value को भी update किया जाता है |
loop के execute होने के बाद length की value को return किया जाता है |
string :: concatenate() मे ,
इस function मे दो string first और last को pass किया जाता है |
concatenate operation के लिए loop चलाया जाता है |
loop मे ‘i’ की value को length + 1 से initial होती है | और ये loop तब तक चलाया जाता है जब तक last मे null pointer नहीं आ जाता है | इस loop मे दूसरा control variable j भी होता है | जिसका initial ‘0’ से किया जाता है |
loop की body मे first [i]=last[j]; statement perform होता है |
loop के बाद first को return किया जाता है |
इस function मे दो string first और second को pass किया जाता है |
compare operation के लिए loop चलाया जाता है |
loop मे ‘i’ की value को ‘0’ से initial होती है | और ये loop तब तक चलाया जाता है जब तक first और second मे null pointer नहीं आ जाता है | इस loop मे i की value को update किया जाता है |
loop की body मे first और second से compare किया जाता है |
अगर first और second के element match होता है जब f की value ‘1’ रहती है अन्यथा
f की value ‘0’ हो जाती है और loop terminate हो जाता है |
loop के बहार f की value को return किया जाता है |
#include<iostream.h>
#include<conio.h>
class string {
private :
int length ;
string first ;
string last ;
string full_name;
public:
int length (char first [] )
void concatenate(string first , string last )
void compare (string 1 , string 2 )
}
string :: length (first)
{
{
int i ;
for(i=0;first[i]!=”\0″;i++)
{
length ++;
}
}
string :: concatenate(string first , string last)
{
int len;
len=length(first);
for(i=length+1 ,j=0;last[j]==’\0′;i++,j++)
{
first[i]=last[j];
}
cout<<“Full name :”<<first;
}
string :: void compare (first , second )
{
int f ;
int i ;
for(i=0 ,j=0;first[i]==’\0′ && first[i]==’\0′;i++)
{
if(first[i]== last [i])
{
f =1 ;
}
else{
f ==0;
}
}
return f ;
}
}
void main()
{
string c;
char first [60],last_name[60];
cout<<“Enter first name :”<<endl;
std.cin(first_name,60);
cout<<“Enter last name :”<<endl;
std.cin(last_name,60);
c.concatenate(first ,last_name);
int l= c.length (first_name);
cout<<“Length :”<<l;
int count = c.compare (first ,last_name);
if(count == 1)
{
cout <<“both string are same “<<endl;
}
else
{
cout<<“both string are not same “<<endl;
}
getch();
}
उदाहरन 2 :
write a program to square and cube function using member function.
सबसे पहले class operation को declare किया जाता है | जिसमे class के लिए variable और member function के prototype को ही delclare किया जाता है |
square () से sqaure operation perform होता है | इसे square (int a) से declare किया जाता है जिसमे a class operation का private variable है |
cube () से cube perform होता है इसे cube (int a) से declare किया जाता है जिसमे a class operation का private variable है |
class scope को close करने के बाद scope resolution से इन सभी function की definition को define किया जाता है |
इसके लिए निन्म syntax होता है :
operation :: square (int a ) {
function _ body ;
}
operation :: cube (int a ) {
function _ body ;
}
main() मे ,
सबसे पहले यूजर से एक value को input किया जाता है | इसके बाद class operation के object को define किया जाता है इस object से class के सभी function को define किया जाता है |
source code
#inclue<iostrem>
#include<conio>
using namespace std;
class operation
{
private :
int a;
private :
void square (int a );
void cube (int a );
void display( int a , int b )
{
cout<<“square value is “<<square (int a )<<endl;
cout<<“cube value is “<<cube (int a )<<endl;
} ; // close of class
peration :: sqaure (int a ) {
return a*a;
}
operation :: cube (int a) {
return a*a*a ;
}
}
void main()
{
int num1 ;
cout<<“enter number :”<<endl;
cin>>num1 ;
intca.display(num1) ;
getch();
}
इसका आउटपुट होगा :
enter number :12
square value is 144
cube value is 1728