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:

Method overloading and overriding in Python

 Overriding:

  • Method overriding is a mechanism in which a subclass inherits the methods of the superclass and sometimes the subclass modifies the implementation of a method defined in the superclass.
  • That means if subclass (Derived class) has the same method as declared in the parent class (base class).
  • Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding:

  • The method must have the same name as in the parent class
  • The method must have the same parameter as in the parent class.
  • There must be inherited.
  • A static method cannot be overridden. It can be proved by runtime polymorphism.

Syntax:

  1. class base_class_name:

    def method_name(parameter_list):

              //Statement

     

    class derived_class_name(base_class_name):

    base_class_method(parameter_list):

              //Statement

Example program:

  1. #method overriding or runtime polymorphism

    class Shape:

        result=0

        def area(self):

            print("\n\tCalculating area of Rectangle,Circle,Triangle .........")

    class Rectangle(Shape):

        def area(self):

            l=int(input("Enter length: "))

            w=int(input("Enter width: "))

            result=l*w; #area=length*width;

            print("Area of rectangle : ",result)

           

    class Circle(Shape):

        def area(self):

            r=float(input("Enter radius  of circle : "))

            result=3.14*r*r; #area=3.14*radius*radius;

            print("Area of circle : ",result)

     

    s=Shape()

    s.area()

    r=Rectangle()

    r.area()

    c=Circle()

    c.area()

Output:

Method overloading:

  • Overloading is a mechanism in which we can use many methods having the same function name but can pass the different numbers of parameters or different types of parameters.
  • In python, doest not call the data types at function definition.
  • So python does not support method overloading.
Share:

Polymorphism in python

 Polymorphism:

  • Poly means many and prism means form so polymorphism means many forms.
  • It is used to perform a single action in different ways.
  • For example,
  • Action is drawn and it performance maybe draw a line or draw a circle.

Compile-time polymorphism:

  • The runtime polymorphism is also called static polymorphism.
  • It is the best example is method overriding.

Example program:

  1. #method overriding or runtime polymorphism

    class Shape:

        result=0

        def area(self):

            print("\n\tCalculating area of Rectangle,Circle,Triangle .........")

    class Rectangle(Shape):

        def area(self):

            l=int(input("Enter length: "))

            w=int(input("Enter width: "))

            result=l*w; #area=length*width;

            print("Area of rectangle : ",result)

           

    class Circle(Shape):

        def area(self):

            r=float(input("Enter radius  of circle : "))

            result=3.14*r*r; #area=3.14*radius*radius;

            print("Area of circle : ",result)

     

    s=Shape()

    s.area()

    r=Rectangle()

    r.area()

    c=Circle()

    c.area()

Output:

Note:

Share:

Encapsulation in C++

Encapsulation:

  • Data encapsulation is used to hide the implementation details from users.
  • If a data member is private it means it can only be accessed within the same class. 
  • No outside class can access private data member (variable) of other class.

Syntax:

  1. class class_name

    {

    private data_members;

    public: methods()

    {

    }

    };

Example program:
  1. #include<iostream.h>

    #include<conio.h>

    class Account {

        private:

         int account_number;

         int account_balance;

        public:

         void show_Data() {

              account_number=13456;

              account_balance=5000;

              cout<<"\nBefore deposit of account XXXXX"<<account_number<<" number of balance :"<<account_balance;

        }

         void deposit(int a) {

              if (a < 0) {

                  cout<<"\nGiven amount is wrong...";

              }

              else{

                     account_balance = account_balance + a;

                     cout<<"\n\nAfter deposit of current balance :"<<account_balance;

              }

        }

    };

    void main()

        {

              clrscr();

              Account ac;

              ac.show_Data();

              ac.deposit(200);

              getch();

        }

Output:
Share:

Method overloading and overriding in C++

 Overriding:

  • Method overriding is a mechanism in which a subclass inherits the methods of the superclass and sometimes the subclass modifies the implementation of a method defined in the super class.
  • That means if subclass (Derived class) has the same method as declared in the parent class (base class).
  • Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding:

  • The method must have the same name as in the parent class
  • The method must have the same parameter as in the parent class.
  • There must be inherited.
  • A static method cannot be overridden. It can be proved by runtime polymorphism.

Syntax:

  1. class base_class_name

    {

    return_type method_name(parameter_list)

    {

              //Statement

    }

    }

     

    class derived_class_name

    {

    base_class_method(parameter_list)

    {

              //Statement

    }

    }

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    class Shape {

    public:

    double a;

        void area() {

    cout<<"\n\tCalculating area of Rectangle,Circle,Triangle .........";

        }

    };


    class Rectangle:public Shape {

       public:

        void area(float l,float w) {

    cout<<"\n\nEnter length and width of rectangle : ";

    cin>>l>>w;

    a=l*w; //area=length*width;

    cout<<"\nArea of rectangle : "<<a;

        }

    };


    class Circle:public Shape {

    public:

        void area(float r) {

    cout<<"\n\nEnter radius  of circle : ";

    cin>>r;

    a=3.14*r*r; //area=3.14*radius*radius;

    cout<<"\nArea of circle : "<<a;

        }

    };


    void main()

    {

    clrscr();

    Shape s;

    s.area();

    Rectangle r;

    r.area(6,3);

    Circle c;

    c.area(3.2);

    getch();

    }

Output:

Method overloading:

  • Overloading is a mechanism in which we can use many methods having the same function name but can pass the different numbers of parameters or different types of parameters.

Syntax:

  1. class base_class_name

    {

    return_type method_name(parameter_list)

    {

              //Statement

    }

    return_type same_method_name(parameter_list)

    {

              //Statement

    }

    }

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    class Shape {

    public:

    double a;

        void area() {

              cout<<"\n\tCalculating area of Rectangle,Circle,Triangle .........";

        }

    };

     

    class Rectangle: public Shape {

       public:

        void area() {

              float l,w;

              cout<<"\n\nEnter length and width of rectangle : ";

              cin>>l>>w;

              a=l*w; //area=length*width;

              cout<<"\nArea of rectangle : "<<a;

        }

    };

     

    class Circle:public Shape {

    public:

        void area() {

              float r;

              cout<<"\n\nEnter radius  of circle : ";

              cin>>r;

              a=3.14*r*r; //area=3.14*radius*radius;

              cout<<"\nArea of circle : "<<a;

        }

    };

     

    void main()

    {

              clrscr();

              Shape s;

              s.area();

              Rectangle r;

              r.area();

              Circle c;

              c.area();

              getch();

    }

Output:
Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me