Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Exceptions Handling
2.1.
Code in C++
2.2.
Code in C++
3.
Member Functions
3.1.
Calling class Member function
4.
Frequently Asked Questions
4.1.
What are classes and objects in C++?
4.2.
What is a virtual function in C++?
4.3.
What are C++ access specifiers?
5.
Conclusion
Last Updated: Mar 27, 2024

Exceptions and Member functions in C++

Introduction

This blog will discuss the concept of exceptions and member functions. First, we will discuss how to catch base and derived classes as exceptions. After that, we will discuss the member function. To catch an exception for both base and derive class then we need to put catch block of derived class before the base class. Otherwise, the catch block of derived class will never be reached..

Fibonacci Series in C++

Exceptions Handling

Here we will see how we can catch base and derived classes as exceptions. If we want to catch exceptions for both base and derived classes, we will have to put the catch block of the base class after the derived class.

Now let us discuss this concept with the help of a suitable example. Here in this example, we have placed the catch block of the derived class before the base class,

Code in C++

#include <bits/stdc++.h>
using namespace std;

class baseClass
{
};

// class derivedClass inherit the class baseClass
class derivedClass : public baseClass
{
}; 
int main()
{
    derivedClass derived;
    try
    {
        throw derived;
    }
    catch (derivedClass derived)
    {
        cout << "Derived Exception"; // catch block of the derived class
    }
    catch (baseClass base)
    {
        cout << "Base Exception"; // catch block of the base class
    }
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Derived Exception

 

Now, we will discuss another example here, we will place the catch block of the derived class after the catch block of the base class.

Code in C++

#include <bits/stdc++.h>
using namespace std;

class baseClass
{
};

// class derivedClass inherit the class baseClass
class derivedClass : public baseClass
{
}; 
int main()
{
    derivedClass derived;
    try
    {
        throw derived;
    }
    catch (baseClass base)
    {
        cout << "Base Exception"; // catch block of the base class
    }
    catch (derivedClass derived)
    {
        cout << "Derived Exception"; // catch block of the derived class
    }
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

warning: exception of type 'derivedClass' will be caught
Base Exception

 

Try and compile with online c++ compiler.

Here as you can see in the output, there is a warning, and hence we cannot reach the catch block of the derived class. Therefore, we need to place the catch block of the derived class before the catch block of the base class.

Member Functions

Member functions are declared within the class definition and work on the class's data members. The definition of member functions can occur either within or outside the class's definition.

If the member function is declared within the class definition, it can be defined directly; however, if it is defined outside the class, we must use the scope resolution:: operator in conjunction with the class name and function name.

For example:

class Square
{
    public:
    int side;
 
    // Declaring function getArea
    // with no argument and return type int.
    int getArea();     
};
You can also try this code with Online C++ Compiler
Run Code

 

If we declare a function inside a class, then we don’t need to declare it beforehand, we can directly define the function inside the class, 

class Square
{
    public:
    int side;
 
    // function getArea returns the area of the square
    int getArea(){
      return side*side; 
    }     
};
You can also try this code with Online C++ Compiler
Run Code

 

If we want to declare a member function outside the class, then we need first define the function inside the class,

class Square
{
    public:
    int side;
    int getArea();   
};

int Square:: getArea(){
   return side*side;
}
You can also try this code with Online C++ Compiler
Run Code

Calling class Member function

Similarly, to access a data member in the class, we will use the dot operator to access the public member functions via the class object (.). 

int main(){
  Square S;
  S.side = 5; // initialising the side value
  cout<<"The area of the square is:"<<S.getArea()<<endl; 
}
You can also try this code with Online C++ Compiler
Run Code

Frequently Asked Questions

What are classes and objects in C++?

A class is a data type that is specified by the user and has data members and member methods. Data members are data variables, and member functions are used to operate on these variables.

An object is a class instance. Because a class is a user-defined data type, an object is sometimes referred to as a variable.

What is a virtual function in C++?

A virtual function is a base class member function that you redefine in a derived class. The virtual keyword is used to declare a virtual function. C++ chooses which function is to be executed at runtime depending on the type of the object referenced by the base class pointer when the function is made virtual.

What are C++ access specifiers?

In C++, there are three types of access specifiers:

Public: All the data members and member methods are accessible outside the class.

Protected: All data members and member methods are accessible inside the class and to the derived class.

Private: All data members and member methods are not accessible outside the class.

Conclusion

This article discussed the concept of exceptions and member functions in C++. We have discussed them using suitable examples to provide clarity to you. I hope you have better understood the above topic after reading this blog. 

C++ vs Java

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!


Live masterclass