Software Development Exam  >  Software Development Notes  >  DSA in C++  >  C++ Classes and Objects

C++ Classes and Objects | DSA in C++ - Software Development PDF Download

Introduction 

In C++, classes and objects are fundamental concepts that form the basis of object-oriented programming. A class is a blueprint or template for creating objects, while an object is an instance of a class. Classes allow us to define our own data types and encapsulate data and methods into a single entity.

Creating a Class

To create a class in C++, we use the 'class' keyword followed by the class name. Let's consider an example where we create a class called Person that represents a person's attributes such as name and age:

class Person {

public:

    std::string name;

    int age;

};

In the above code, we declare two member variables: 'name' of type 'std::string' and 'age' of type 'int'. The 'public': keyword denotes that these variables can be accessed from outside the class.

Creating Objects

Once we have defined a class, we can create objects of that class. Objects are instances of the class and hold the actual data. To create an object, we use the class name followed by the object name and optional parentheses:

Person person1;

In the above code, we create an object named 'person1' of type 'Person'.

Accessing Class Members

We can access the member variables and member functions of a class using the dot ('.') operator. Let's see an example:

person1.name = "John";

person1.age = 25;

In the above code, we access the 'name' and 'age' member variables of the 'person1' object and assign values to them.

Member Functions

Classes can also have member functions, which are functions defined inside the class. These functions can operate on the member variables of the class. Let's add a member function called 'displayDetails' to our 'Person' class:

class Person {

public:

    std::string name;

    int age;


    void displayDetails() {

        std::cout << "Name: " << name << std::endl;

        std::cout << "Age: " << age << std::endl;

    }

};

The 'displayDetails' function prints the name and age of the person. We can call this function on an object of the 'Person' class as follows:

person1.displayDetails();

The output will be:

Name: John

Age: 25

Constructors

class Person {

public:

    std::string name;

    int age;


    Person(const std::string& n, int a) {

        name = n;

        age = a;

    }


    void displayDetails() {

        std::cout << "Name: " << name << std::endl;

        std::cout << "Age: " << age << std::endl;

    }

};

In the above code, we define a constructor that takes the 'name' and 'age' as parameters and initializes the member variables. We can now create objects of the 'Person' class using this constructor:

Person person1("John", 25);

person1.displayDetails();

The output will be the same as before:

Name: John

Age: 25

Sample Problems

Problems 1: Create a class called 'Rectangle' with member variables 'width' and 'height'. Add a member function 'calculateArea' that calculates and returns the area of the rectangle.

class Rectangle {

public:

    int width;

    int height;


    int calculateArea() {

        return width * height;

    }

};

Problems 2: Create a class called 'Circle' with member variable 'radius'. Add a constructor that initializes the radius. Add a member function 'calculateArea' that calculates and returns the area of the circle.

class Circle {

public:

    double radius;


    Circle(double r) {

        radius = r;

    }


    double calculateArea() {

        return 3.14159 * radius * radius;

    }

};

Conclusion

In this article, we explored the concepts of classes and objects in C++. We learned how to create a class, create objects of that class, access class members, define member functions, and use constructors. Classes and objects provide a powerful way to organize and manipulate data in object-oriented programming. By understanding these concepts, you can build complex data structures and algorithms in the field of data structures and algorithms in C++.

The document C++ Classes and Objects | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Important questions

,

C++ Classes and Objects | DSA in C++ - Software Development

,

C++ Classes and Objects | DSA in C++ - Software Development

,

Previous Year Questions with Solutions

,

Extra Questions

,

shortcuts and tricks

,

Semester Notes

,

study material

,

ppt

,

Sample Paper

,

MCQs

,

pdf

,

Exam

,

Viva Questions

,

C++ Classes and Objects | DSA in C++ - Software Development

,

Summary

,

video lectures

,

Objective type Questions

,

mock tests for examination

,

practice quizzes

,

past year papers

,

Free

;