[go: up one dir, main page]

0% found this document useful (0 votes)
51 views15 pages

OBJECT ORIENTED PROGRAMMING IN C++

oops

Uploaded by

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

OBJECT ORIENTED PROGRAMMING IN C++

oops

Uploaded by

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

OBJECT ORIENTED PROGRAMMING IN C++

MODULE 1:

1. What is Object-Oriented Programming? Explain the Structure of a C++ Program.

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design software.
Objects can represent real-world entities and encapsulate data and behaviors (methods). OOP promotes
concepts such as encapsulation, inheritance, polymorphism, and abstraction.

Structure of a C++ Program: A typical C++ program consists of the following components:

• Header Files: Include necessary libraries.

• Namespace: Typically, std for standard functions.

• Class Definitions: Define classes with data members and methods.

• Main Function: The entry point of the program.

#include <iostream>

using namespace std;

class MyClass {

public:

void display() {

cout << "Hello, World!" << endl;

};

int main() {

MyClass obj;

obj.display();

return 0;

2. Salient Features of Object-Oriented Programming Languages

• Encapsulation: Bundling data and methods that operate on the data within a single unit (class).

• Inheritance: Mechanism to create new classes from existing classes, enabling code reuse.
• Polymorphism: Ability to process objects differently based on their data type or class.

• Abstraction: Hiding complex implementation details and exposing only the necessary parts of an
object.

• 3. What are Classes and Objects? Explain with an Example.


• Classes are blueprints for creating objects. They encapsulate data and methods. Objects
are instances of classes.
• Example:

class Car {

public:

string brand;

int year;

void display() {

cout << "Brand: " << brand << ", Year: " << year << endl;

};

int main() {

Car myCar; // Create an object of Car

myCar.brand = "Toyota";

myCar.year = 2020;

myCar.display(); // Output: Brand: Toyota, Year: 2020

return 0;

4. What is a Friend Function? Explain with an Example.

A friend function is a function that is not a member of a class but has access to its private and protected
members. It is declared inside the class with the friend keyword.

Example:

class Box {

private:
int length;

public:

Box(int len) : length(len) {}

friend void printLength(Box b); // Friend function declaration

};

void printLength(Box b) {

cout << "Length: " << b.length << endl; // Accessing private member

int main() {

Box box(10);

printLength(box); // Output: Length: 10

return 0;

5. What are Constructors and Destructors? Explain the Scope Resolution Operator with an Example.

Constructors are special member functions called when an object is created, used to initialize the object.
Destructors are called when an object is destroyed, used for cleanup.

The scope resolution operator (::) is used to define or access members of a class outside its body.

Example:

class Example {

public:

Example() { // Constructor

cout << "Constructor called!" << endl;

~Example() { // Destructor

cout << "Destructor called!" << endl;


}

};

int main() {

Example obj; // Constructor called

return 0; // Destructor called when obj goes out of scope

6. How Objects are Passed to Functions? Explain with an Example.

Objects can be passed to functions by value or by reference. Passing by value creates a copy, while
passing by reference allows the function to modify the original object.

Example:

class Rectangle {

public:

int width, height;

Rectangle(int w, int h) : width(w), height(h) {}

};

// Pass by value

void printArea(Rectangle rect) {

cout << "Area: " << rect.width * rect.height << endl;

// Pass by reference

void scale(Rectangle &rect) {

rect.width *= 2;

rect.height *= 2;

int main() {
Rectangle rect(3, 4);

printArea(rect); // Area: 12

scale(rect);

printArea(rect); // Area: 24

return 0;

7. Explain Parameterized Constructors with an Example.

Parameterized constructors allow passing parameters to initialize object attributes at the time of
creation.

Example:

class Circle {

public:

double radius;

Circle(double r) : radius(r) {} // Parameterized constructor

double area() {

return 3.14 * radius * radius;

};

int main() {

Circle circle(5.0); // Creating an object with radius 5.0

cout << "Area: " << circle.area() << endl; // Output: Area: 78.5

return 0;

8. Develop a C++ Program to Find the Largest of Three Numbers.

#include <iostream>

using namespace std;


int main() {

int a, b, c;

cout << "Enter three numbers: ";

cin >> a >> b >> c;

int largest = a; // Assume a is the largest

if (b > largest) largest = b;

if (c > largest) largest = c;

cout << "The largest number is: " << largest << endl;

return 0;

9. Write a C++ Program to Demonstrate Accessing of Data Members.

#include <iostream>

using namespace std;

class Student {

public:

string name;

int age;

void display() {

cout << "Name: " << name << ", Age: " << age << endl;

};

int main() {

Student student;
student.name = "Alice";

student.age = 20;

student.display(); // Output: Name: Alice, Age: 20

return 0;

MODULE 2:

1. Explain Array of Pointers to Objects

An array of pointers to objects is a collection where each element is a pointer pointing to an object of a
class. This allows you to manage multiple objects dynamically.

Example:

#include <iostream>

using namespace std;

class Student {

public:

string name;

Student(string n) : name(n) {}

void display() {

cout << "Name: " << name << endl;

};

int main() {

const int SIZE = 3;

Student* students[SIZE]; // Array of pointers to Student objects

// Dynamically creating Student objects

for (int i = 0; i < SIZE; ++i) {

string name;
cout << "Enter name for student " << i + 1 << ": ";

cin >> name;

students[i] = new Student(name); // Allocating memory

// Displaying student names

for (int i = 0; i < SIZE; ++i) {

students[i]->display();

delete students[i]; // Cleaning up memory

return 0;

2. Explain Accessing Member Functions Using Pointers

You can access member functions of a class using pointers to the object. The -> operator is used for this
purpose.

Example:

#include <iostream>

using namespace std;

class Rectangle {

public:

int width, height;

Rectangle(int w, int h) : width(w), height(h) {}

int area() {

return width * height;

}
};

int main() {

Rectangle* rect = new Rectangle(5, 10); // Pointer to a Rectangle object

cout << "Area: " << rect->area() << endl; // Accessing member function

delete rect; // Clean up

return 0;

3. Explain the Following Terms with an Example

(i) Pointers to Class Members

Pointers to class members allow you to point to a member variable or member function of a class.

Example:

#include <iostream>

using namespace std;

class MyClass {

public:

void display() {

cout << "Hello from MyClass!" << endl;

};

int main() {

MyClass obj;

void (MyClass::*funcPtr)() = &MyClass::display; // Pointer to member function

(obj.*funcPtr)(); // Calling the member function using pointer

return 0;

(ii) The this Pointer


The this pointer is an implicit pointer available in non-static member functions, pointing to the object for
which the member function is called.

Example:

#include <iostream>

using namespace std;

class MyClass {

public:

int value;

MyClass(int v) : value(v) {}

void show() {

cout << "Value: " << this->value << endl; // Using this pointer

};

int main() {

MyClass obj(10);

obj.show(); // Output: Value: 10

return 0;

4. What is Upcasting? Explain with an Example.

Upcasting is the process of converting a pointer or reference of a derived class to a pointer or reference
of a base class. It is safe and allows you to treat derived class objects as base class objects.

Example:

#include <iostream>

using namespace std;


class Base {

public:

void show() {

cout << "Base class" << endl;

};

class Derived : public Base {

public:

void show() {

cout << "Derived class" << endl;

};

int main() {

Derived d;

Base* b = &d; // Upcasting

b->show(); // Output: Base class (if show() is not virtual)

return 0;

5. Write a C++ Program to Demonstrate Function Overloading

#include <iostream>

using namespace std;

class Math {

public:

int add(int a, int b) {

return a + b;

}
double add(double a, double b) {

return a + b;

};

int main() {

Math math;

cout << "Integer addition: " << math.add(5, 3) << endl; // Output: 8

cout << "Double addition: " << math.add(5.5, 3.3) << endl; // Output: 8.8

return 0;

6. Explain User Defined Copy Constructors with a C++ Program

A user-defined copy constructor is defined to initialize an object using another object of the same class.

Example:

#include <iostream>

using namespace std;

class MyClass {

public:

int value;

// Parameterized constructor

MyClass(int v) : value(v) {}

// User-defined copy constructor

MyClass(const MyClass &obj) {

value = obj.value;

}
void display() {

cout << "Value: " << value << endl;

};

int main() {

MyClass obj1(10); // Calls parameterized constructor

MyClass obj2 = obj1; // Calls copy constructor

obj1.display(); // Output: Value: 10

obj2.display(); // Output: Value: 10

return 0;

7. Write Various Function Overloading to Demonstrate “Ambiguity”

Ambiguity in function overloading occurs when the compiler cannot determine which function to call.

Example:

#include <iostream>

using namespace std;

class Ambiguity {

public:

void show(int a) {

cout << "Integer: " << a << endl;

void show(double a) {

cout << "Double: " << a << endl;

};
int main() {

Ambiguity obj;

// obj.show(5); // This is not ambiguous

// obj.show(5.0); // This is not ambiguous

// Ambiguous call

// obj.show(5); // Uncommenting this will cause an ambiguity error

return 0;

8. Develop a C++ Program Using Operator Overloading for Overloading Unary Minus Operator

#include <iostream>

using namespace std;

class Number {

public:

int value;

Number(int v) : value(v) {}

// Overloading unary minus operator

Number operator-() {

return Number(-value);

void display() {

cout << "Value: " << value << endl;

};
int main() {

Number num(5);

num.display(); // Output: Value: 5

Number negNum = -num; // Using overloaded operator

negNum.display(); // Output: Value: -5

return 0;

You might also like