Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

#include <iostream>

#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.

Example 2: C++ Parameterized Constructor


// C++ program to calculate the area of a wall

#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);

cout << "Area of Wall 1: " << wall1.calculateArea() << endl;


cout << "Area of Wall 2: " << wall2.calculateArea() << endl;

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.

A destructor is defined like constructor. It must have same name


as class. But it is prefixed with a tilde sign (~).

Note: C++ destructor cannot have parameters. Moreover,


modifiers can't be applied on destructors.
C++ Constructor and Destructor Example
Let's see an example of constructor and destructor in C++ which is
called automatically.
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
return 0;
}
Output:

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.

In C++, the class which inherits the members of another class is


called derived class and the class whose members are inherited is
called base 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:

class derived_class_name :: visibility-mode base_class_name


{
// body of the derived class.
}

derived_class_name: It is the name of the derived class.


visibility mode: The visibility mode specifies whether the features of the
base class are publicly inherited or privately inherited. It can be public or
private.
base_class_name: It is the name of the base class
#include <iostream>
using namespace std;
class Employee { Output:
public: Salary: 60000
float salary = 60000; Bonus: 5000
}; In the above example,
class Programmer: public Employee { Employee is the base
public: class and Programmer is
float bonus = 5000; the derived class.
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;

//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();

// Calling member of the derived class


dog1.bark();

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;
}
};

You might also like