C++ classes with static members, methods with default arguments and friend functions


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpg
 
Implementation of Static members, Default arguments and Friend functions

Aim
        To design a C++ classes with static members, methods with default arguments and friend functions.

Algorithm

Default arguments:

Step 1: Start the Program Execution
Step 2:Create the Classes  and declare all the Variables and Member functions.
Step 3: Function may be defined to take default values for the omitted arguments
           by providing the default values in the function prototype.
Step 4: Declare the default function.
Step 5: Invoke the default function.
Step 6: Display the result
Step 7:Stop the Program Execution.

Static Member Functions:

Step 1: Start the Program Execution
Step 2: Create the Classes and declare all the Variables and Member functions.
Step 3: Static member function of class is shared by all the instances of the class. A static                 
             member functions cannot access auto members of a class.
Step 4: Create class TEST with static member function.
Step 5: Invoke the static member function using class name with scope
            Resolution operator and function name.
Step 6: Display the value
Step 7: Stop the Program Execution.

Static Data members:

Step 1: Start the Program Execution
Step 2: Create the Classes and declare all the Variables and Member functions.
Step 3: Static data member of class is shared by all the instances of the class.
Step 4: Create class ITEM with static data member as count.
Step 5: Create a member function to increment the count.
Step 6: Declare the static data member using scope resolution operator.
Step 7: Display the count value.
Step 8: Stop the Program Execution.

Friend functions:
Step 1: Start the Program Execution
Step 2: Create the Classes and declare all the Variables and Member functions.
Step 3: Friend function is used to access the private data member and member
             Function.
Step 4: Create the class and declare the data member as private.
Step 5: Declare the friend function using the keyword friend.
Step 6: Perform the operation
Step 7: Display the result.
Step 8: Stop the Program Execution.

 
Program:
Default arguments:
Static Data members:
#include<iostream.h>
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<”Number”<<num;
}
void showcount()
{
cout<<”count”;
cout<<count<<”\n”;
}
};
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
c.getdata(40);
a.showcount();
b.showcount();
c.showcount();
}



OUTPUT:
count 0
count 0
count 0
Number 20
Number 30
Number 40
count 3
count 3
count 3




Static Member Functions:
#include
class test
{
int code;
static int count;
public:
void setcode()
{
cout<<”Object Number:”<<code<<”\n”;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}



OUTPUT:

count 2
count 3
Object Number 1
Object Number 2
Object Number 3
Friend functions:

0 comments:

Post a Comment