0% found this document useful (0 votes)
57 views6 pages

Ds Lab Program

The document discusses several OOP concepts in C++ including: 1. Creating multiple objects of a class and accessing object attributes and methods. 2. Defining classes with constructor parameters to initialize object attributes. 3. Inheritance, where a derived class inherits attributes and methods from a base class. 4. Polymorphism, where derived classes can have different implementations of the same method from the base class. 5. Encapsulation through private/public access specifiers to hide sensitive class details. 6. Exception handling using try, throw, and catch keywords to handle runtime errors.

Uploaded by

SANA MATEEN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views6 pages

Ds Lab Program

The document discusses several OOP concepts in C++ including: 1. Creating multiple objects of a class and accessing object attributes and methods. 2. Defining classes with constructor parameters to initialize object attributes. 3. Inheritance, where a derived class inherits attributes and methods from a base class. 4. Polymorphism, where derived classes can have different implementations of the same method from the base class. 5. Encapsulation through private/public access specifiers to hide sensitive class details. 6. Exception handling using try, throw, and catch keywords to handle runtime errors.

Uploaded by

SANA MATEEN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

create multiple objects of one class:

// Create a Car class with some attributes


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

Output;

BMW X5 1999
Ford Mustang 1969

Parameters with methods


#include <iostream>

using namespace std;

class Car {
  public:
    int speed(int maxSpeed);
};

int Car::speed(int maxSpeed) {
  return maxSpeed;
}

int main() {
  Car myObj; // Create an object of Car
  cout << myObj.speed(200); // Call the method with an argument
  return 0;
}

output

200

Constructor Parameters
  class Car {        // The class
  public:          // Access specifier
    string brand;  // Attribute
    string model;  // Attribute
    int year;      // Attribute
    Car(string x, string y, int z) { // Constructor with parameters
      brand = x;
      model = y;
      year = z;
    }
};

int main() {
  // Create Car objects and call the constructor with different values
  Car carObj1("BMW", "X5", 1999);
  Car carObj2("Ford", "Mustang", 1969);

  // Print values


  cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
  cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
  return 0;
}

output:

BMW X5 1999
Ford Mustang 1969
Access Specifiers

class MyClass {
  public:    // Public access specifier
    int x;   // Public attribute
  private:   // Private access specifier
    int y;   // Private attribute
};

int main() {
  MyClass myObj;
  myObj.x = 25;  // Allowed (public)
  myObj.y = 50;  // Not allowed (private)
  return 0;
}

output:

In function 'int main()':


Line 8: error: 'int MyClass::y' is private
Line 14: error: within this context

Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must declare class variables/attributes as private (cannot be accessed from
outside the class). If you want others to read or modify the value of a private member, you can
provide public get and set methods.

#include <iostream>
using namespace std;

class Employee {
  private:
    // Private attribute
    int salary;

  public:
    // Setter
    void setSalary(int s) {
      salary = s;
    }
    // Getter
    int getSalary() {
      return salary;
    }
};
int main() {
  Employee myObj;
  myObj.setSalary(50000);
  cout << myObj.getSalary();
  return 0;
}

Output:

50000

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

class Vehicle {
  public:
    string brand = "Ford";
    void honk() {
      cout << "Tuut, tuut! \n" ;
    }
};

// Derived class
class Car: public Vehicle {
  public:
    string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}

output:

Tuut, tuut!
Ford Mustang
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related
to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods


from another class. Polymorphism uses those methods to perform different tasks. This allows
us to perform a single action in different ways.

For example, think of a base class called Animal that has a method called animalSound().


Derived classes of Animals could be horse, Cats, Dogs, Birds - And they also have their own
implementation of an animal sound (the pig oinks, and the cat meows, etc.):

class Animal {
  public:
    void animalSound() {
    cout << "The animal makes a sound \n" ;
  }
};

// Derived class
class Horse : public Animal {
  public:
    void animalSound() {
    cout << "The pig says: wee wee \n" ;
   }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
    cout << "The dog says: bow wow \n" ;
  }
};

int main() {
  Animal myAnimal;
  Pig myhorse;
  Dog myDog;

  myAnimal.animalSound();
  myHorse.animalSound();
  myDog.animalSound();
  return 0;
}

Exception handling in C++ consist of three keywords: try, throw and catch:

The try statement allows you to define a block of code to be tested for errors while it is being
executed.

The throw keyword throws an exception when a problem is detected, which lets us create a


custom error.
The catch statement allows you to define a block of code to be executed, if an error occurs in
the try block.

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

You might also like