Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

CLASSES

1)create multiple objects of one class:

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;

// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
OUTPUT:
Volume of Box1 : 210
Volume of Box2 : 1560
 

Constructors
class Point

{private:

    int x, y;

 public:
    // Parameterized Constructor

    Point(int x1, int y1)

    {  x = x1;

        y = y1;

    } 

    int getX()

    {        return x;    }

    int getY()

    {        return y;    }

};

 int main()

{    // Constructor called

    Point p1(10, 15);

     // Access values assigned by constructor

    cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

     return 0;

}
Output: 
p1.x = 10, p1.y = 15

Inheritance
 derived class (child) - the class that inherits from another class
 base class (parent) - the class being inherited from

//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

Polymorphism
FUNCTION OVERLOADING

// Function with 2 int parameters


int sum(int num1, int num2) {
return num1 + num2;
}

// Function with 2 double parameters


double sum(double num1, double num2) {
return num1 + num2;
}

// Function with 3 int parameters


int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}

int main() {
// Call function with 2 int parameters
cout << "Sum 1 = " << sum(5, 6) << endl;

// Call function with 2 double parameters


cout << "Sum 2 = " << sum(5.5, 6.6) << endl;

// Call function with 3 int parameters


cout << "Sum 3 = " << sum(5, 6, 7) << endl;

return 0;
}

Output
Sum 1 = 11
Sum 2 = 12.1
Sum 3 = 18

OPERATOR OVERLOADING

// C++ program to overload ++ when used as prefix

#include <iostream>
using namespace std;
class Count {
private:
int value;

public:

// Constructor to initialize count to 5


Count() : value(5) {}

// Overload ++ when used as prefix


void operator ++() {
value = value + 1;
}

void display() {
cout << "Count: " << value << endl;
}
};

int main() {
Count count1;

// Call the "void operator ++()" function


++count1;

count1.display();
return 0;
}
Output
Count: 6

DYNAMIC MEMORY ALLOCATION

#include <iostream>
using namespace std;

class Box {
public:
Box() {
cout << "Constructor called!" <<endl;
}
~Box() {
cout << "Destructor called!" <<endl;
}
};
int main() {
Box B1=new Box();
Delete B1;
Box* myBoxArray = new Box[3];
delete [] myBoxArray; // Delete array

return 0;
}

OUTPUT:
Constructor called!
Constructor called!
Constructor called!
Constructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called

FUNCTION OVERRIDING

// C++ program to demonstrate function overriding

#include <iostream>
using namespace std;

class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};

class Derived : public Base {


public:
void print() {
cout << "Derived Function" << endl;
}
};

int main() {
Derived derived1;

// Call print() function of Derived class


derived1.print();
return 0;
}
Output
Derived Function

FUNCTION TEMPLATE

// If two characters are passed to function template, character with larger ASCII value is displayed.

#include <iostream>
using namespace std;

// template function
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}

int main()
{
int i1, i2;
float f1, f2;
char c1, c2;

cout << "Enter two integers:\n";


cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;

cout << "\nEnter two floating-point numbers:\n";


cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;

cout << "\nEnter two characters:\n";


cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";

return 0;
}
Output
Enter two integers:
5
10
10 is larger.

Enter two floating-point numbers:


12.4
10.2
12.4 is larger.

Enter two characters:


z
Z
z has larger ASCII value

CLASS TEMPLATE
#include <iostream>
using namespace std;

template <class T>


class Calculator
{
private:
T num1, num2;

public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}

void displayResult()
{
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << "Addition is: " << add() << endl;
cout << "Subtraction is: " << subtract() << endl;
cout << "Product is: " << multiply() << endl;
cout << "Division is: " << divide() << endl;
}

T add() { return num1 + num2; }

T subtract() { return num1 - num2; }

T multiply() { return num1 * num2; }

T divide() { return num1 / num2; }


};

int main()
{
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Int results:" << endl;
intCalc.displayResult();

cout << endl << "Float results:" << endl;


floatCalc.displayResult();

return 0;
}
Output
Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

Exception handling
Ex.1 using namespace std;
 
int main()
{
   int x = -1;
 
   // Some code
   cout << "Before try \n";
   try {
      cout << "Inside try \n";
      if (x < 0)
      {
         throw x;
         cout << "After throw (Never executed) \n";
      }
   }
   catch (int x ) {
      cout << "Exception Caught \n";
   }
 
   cout << "After catch (Will be executed) \n";
   return 0;
}
Output: 
Before try
Inside try
Exception Caught
After catch (Will be executed)

Ex.2

try {
  int age = 15;
  if (age >= 18) {
    cout << "Access granted - you are old enough.";
  } else {
    throw (age);
 }
}
catch (int myNum) {
  cout << "Access denied - You must be at least 18 years old.\n";
  cout << "Age is: " << myNum;
}

int age = 20;

You might also like