Class with scope resolution in c++

Scope resolution operator in c++:

How to give function definition at out of the class in c++?

How to use scope resolution operator in c++ using class?

  • The scope resolution operator is used to access data members and member function at the out of the classes.
  • It mainly used to return value at the class.
  • This operator symbol is::( double colon).

Syntax:

  1. class class_name

    {

    Access_specifier:

    return type function_declaration(parameter_list);

    };

     

    //function definition

    return_type class_name :: fuction_name(parameter_list)

    {

              //function body

    }

     

    void main()

    {

              function_calling(parameter_list);

    }

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    #include<iomanip.h>

    class std

    {

    public:

    //function declaration

    int total(int,int,int,int,int);

    float average(int);

    void display(int ,char [], int ,int ,int ,int ,int ,int ,float );

    };

     

    int std :: total(int m1,int m2, int m3,int m4,int m5)

    {

    int tot;

    tot = m1+m2+m3+m4+m5;

    return tot;

    }

     

    float std :: average(int tot)

    {

    float avg;

    avg =tot/5;

    return avg;

    }

     

    void std ::  display(int sno,char *name, int m1,int m2,int m3,int m4,int m5,int tot,float avg)

    {

    cout<<"\n Student details............";

    cout<<"\n Student id is : "<<sno;

    cout<<"\n Student name is : "<<name;

    cout<<"\n Five subject marks : "<<m1<<setw(5)<<m2<<setw(5)<<m3<<setw(5)<<m4<<setw(5)<<m5;

    cout<<"\n Total mark for five subject :"<<tot;

    cout<<"\n Average mark for five subject :"<<avg;

    }

     

    void main()

    {

    int sno,m1,m2,m3,m4,m5,tot;

    float avg;

    char name[30];

    clrscr();

    cout<<"\n Enter student number : ";

    cin>>sno;

    cout<<"\n Enter student name : ";

    cin>>name;

    cout<<"\n Enter Five subject mark : ";

    cin>>m1>>m2>>m3>>m4>>m5;

    std s;//object declaration

    tot=s.total(m1,m2,m3,m4,m5);

    avg=s.average(tot);

    s.display(sno,name,m1,m2,m3,m4,m5,tot,avg);

    getch();

    }

Output:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me