Professional Documents
Culture Documents
#Include #Include Using Namespace STD Class Car (Public: String Brand String Model Int Year )
#Include #Include Using Namespace STD Class Car (Public: String Brand String Model Int Year )
#include<string>
using namespace std;
class Car
{ public:
string brand;
string model;
int year;
};
int main()
{
// Create an object of Car
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
Create another object of Car
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " <<
carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " <<
carObj2.year << "\n"; return 0;
}
C++ Constructors
A constructor is a special type of member function that is called
automatically when an object is created. A constructor has the same
name as that of the class and it does not have a return type. For
example,
class Wall {
public:
// create a constructor
Wall() {
// code
}
};
#include <iostream>
using namespace std;
class Wall {
private:
double length;
public:
// create a constructor
Wall() {
// initialize private variables
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
// create an object
Wall wall1;
return 0;
}
Output:
Creating a Wall
Length = 5.5
C++ Parameterized Constructor
In C++, a constructor with parameters is known as a
parameterized constructor.
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// create parameterized constructor
Wall(double len, double hgt) {
// initialize private variables
length = len;
height = hgt;
}
double calculateArea()
{
return length * height;
}
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
return 0;
}
C++ Destructor
A destructor works opposite to constructor; it destructs the
objects of classes. It can be defined only once in a class. Like
constructors, it is invoked automatically.
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked
C++ Inheritance
In C++, inheritance is a process in which one object acquires all
the properties and behaviors of its parent object automatically. In
such way, you can reuse, extend or modify the attributes and
behaviors which are defined in other class.
derived class (child) - the class that inherits from another class
base class (parent) - the class being inherited from
To inherit from a class, use the : symbol.
Derived Classes
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
//Base class
class Parent
{
public:
int id_p;
};
// Sub class inheriting from Base Class(Parent)
class Child : public Parent
{
public:
int id_c;
};
//main function
int main()
{
Child obj1;
// An object of class child has all data members
// and member functions of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;
return 0;
}
Output:
Child id is 7
Parent id is 91
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();
return 0;
}
Passing objects to a function
In C++ programming, we can pass objects to a function in a similar manner
as passing regular arguments.
C++ Pass Objects to Function
// C++ program to calculate the average marks of two students
#include <iostream>
using namespace std;
class Student {
public:
double marks;
// constructor to initialize marks
Student(double m) {
marks = m;
}
};