Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Class Program
C++ classes are an essential part of object-oriented programming (OOP). They provide a blueprint or template for creating objects with specific attributes and behaviors. In this article, we will explore how to create a C++ class program with examples.
1. Creating a Class
To create a class in C++, you use the class
keyword followed by the class name. Here's an example:
#include <iostream>
#include <string>
// Define a class
class Person {
private:
std::string name;
int age;
public:
// Constructor
Person(const std::string& _name, int _age) : name(_name), age(_age) {}
// Member function
void displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
int main() {
// Create an object of the class
Person person("John Doe", 25);
// Call the member function
person.displayInfo();
return 0;
}
In the above code, we define a class named Person
with two private member variables: name
and age
. We also have a constructor that initializes these variables. Additionally, we have a member function displayInfo()
that outputs the person's name and age. In the main()
function, we create an object of the class Person
and call its displayInfo()
member function.
2. Access Specifiers
C++ provides access specifiers (public
, private
, and protected
) to control the access to class members. Here's an example:
#include <iostream>
#include <string>
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double _length, double _width) : length(_length), width(_width) {}
double calculateArea() {
return length * width;
}
};
int main() {
Rectangle rectangle(5.0, 3.0);
double area = rectangle.calculateArea();
std::cout << "Area: " << area << std::endl;
return 0;
}
In the above code, we define a class Rectangle
with two private member variables: length
and width
. We also have a constructor and a member function calculateArea()
that calculates and returns the area of the rectangle. In the main()
function, we create an object of the class Rectangle
and call its calculateArea()
member function.
3. Class Inheritance
Inheritance allows you to create a new class based on an existing class. The derived class inherits the properties and behaviors of the base class. Here's an example:
#include <iostream>
#include <string>
class Animal {
public:
void makeSound() {
std::cout << "The animal makes a sound." << std::endl;
}
};
class Dog : public Animal {
public:
void makeSound() {
std::cout << "The dog barks." << std::endl;
}
};
int main() {
Animal animal;
animal.makeSound();
Dog dog;
dog.makeSound();
return 0;
}
In the above code, we define a base class Animal
with a member function makeSound()
. We then define a derived class Dog
that inherits from the Animal
class. The Dog
class overrides the makeSound()
member function. In the main()
function, we create objects of both classes and call the makeSound()
member function for each object.
C++ classes provide a structured and modular way to organize your code. By encapsulating data and behavior within a class, you can create objects that possess specific attributes and actions. Utilize classes to design and implement complex systems effectively in your C++ programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners