0% found this document useful (0 votes)
4 views

Assignment_1[1]

The document outlines an assignment for a course on Object Oriented Programming and C++ for the academic year 2024-25. It includes tasks such as writing about the advantages and applications of C++, correcting code syntax errors, and explaining key concepts like classes, objects, polymorphism, and inheritance. Additionally, it differentiates between classes and structures, discusses encapsulation, and lists access specifiers with examples.

Uploaded by

ayushteli80
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)
4 views

Assignment_1[1]

The document outlines an assignment for a course on Object Oriented Programming and C++ for the academic year 2024-25. It includes tasks such as writing about the advantages and applications of C++, correcting code syntax errors, and explaining key concepts like classes, objects, polymorphism, and inheritance. Additionally, it differentiates between classes and structures, discusses encapsulation, and lists access specifiers with examples.

Uploaded by

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

Rayat Shikshan Sanstha’s

S. M. Joshi College, Hadapsar, Pune - 28


S.Y.B.C.A(science) (2024-25)

Subject: Object Oriented Programming and C++ (Semester- IV)

Assignment 1

Date: 17/01/2025

1. Write the advantages and Applications of C+


2. Trace the output and justify
# include < iostream.h>
int x;
main ( ) {
int x = 10;
cout <<< : : x; cout <<< x;
}

Sol ) Code:
#include <iostream.h>
int x;
main() {
int x = 10;
cout <<< : : x;
cout <<< x;
}

Key Observations:

1. Syntax Issues:
o The #include <iostream.h> header is not valid in modern C++. It should be #include
<iostream>.
o The main() function is missing a return type. It should be int main().
o The insertion operator << is incorrectly written as <<<. This would cause a compilation error.
2. Scope of x:
o The int x declared at the global scope is uninitialized and holds an undefined value.
o Inside main, a local variable x is declared and initialized to 10. This x shadows the global x
within the main function's scope.
3. Incorrect Use of :::
o The ::x syntax is used to access the global variable x in C++. However, it is being used
incorrectly in combination with the broken syntax of <<<.
4. Output Issues:
o Even if the syntax were corrected, accessing the uninitialized global variable x via ::x would
result in undefined behavior (UB). The output for this part would depend on the garbage value in
x.

Corrected Code:

Here's a corrected and functional version of the code:

#include <iostream>
using namespace std;

int x; // Global variable

int main() {
int x = 10; // Local variable
cout << ::x << endl; // Access global x (uninitialized, may give 0 or
garbage based on the compiler)
cout << x << endl; // Access local x (10)
return 0;
}
3. Write 4 differences between Procedure oriented programming and object
oriented Programming.
4. Define the following i) Object ii) Class iii) Polymorphism

i) Object

An object is an instance of a class and represents a real-world entity in object-oriented


programming (OOP). Objects combine data (attributes or properties) and behavior (methods or
functions) into a single unit. Each object is unique and can interact with other objects in the
program.

ii) Class

A class is a blueprint or template used to create objects. It defines the structure and behavior of
the objects by specifying their attributes (data members) and methods (member functions).
However, a class by itself does not occupy memory until an object is created.

 Syntax:

class ClassName {
public:
// Data members
// Member functions
};

 Example:

class Car {
public:
string brand;
int year;

void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};

int main() {
Car myCar; // Object creation
myCar.brand = "BMW";
myCar.year = 2022;
myCar.display(); // Calling a method
return 0;
}

In this example, myCar is an object of the class Car.


iii) Polymorphism

Polymorphism is a key concept in OOP that allows methods or functions to behave differently
based on the object or data type. It means "many forms" and enables the same function or
method to work in various ways.

Types of Polymorphism:

1. Compile-Time Polymorphism (Static Binding):


o Achieved using function overloading or operator overloading.
o The behavior is determined at compile-time.
2. Run-Time Polymorphism (Dynamic Binding):
o Achieved using virtual functions and inheritance.
o The behavior is determined at runtime.

OR
5. Which operators are used as Memory Management Operators? Write the
syntax ( NEW and delete operator)
6. List the different access specifier with example

Access Specifiers
1. Public: Members can be accessed from anywhere.
2. Private: Members can be accessed only within the class.
3. Protected: Members can be accessed within the class and derived classes.

Example:
#include <iostream>
using namespace std;

class MyClass {
public:
int publicVar; // Accessible from anywhere

private:
int privateVar; // Accessible only within the class

protected:
int protectedVar; // Accessible within the class and derived classes
};

int main() {
MyClass obj;
obj.publicVar = 10; // Accessible because it's public
// obj.privateVar = 20; // Error: private member
// obj.protectedVar = 30; // Error: protected member

cout << "Public Variable: " << obj.publicVar << endl;


return 0;
}

Output:
Public Variable: 10
7. What is Encapsulation? Explain with example.
Encapsulation is one of the fundamental principles of OOP that involves bundling the data (variables) and the
methods (functions) that operate on the data into a single unit, usually a class.

#include <iostream>
using namespace std;

class Student {
private:
int rollNo; // Private data member
string name;

public:
// Setter methods to set the values
void setRollNo(int r) {
rollNo = r;
}

void setName(string n) {
name = n;
}

// Getter methods to get the values


int getRollNo() {
return rollNo;
}

string getName() {
return name;
}

// Method to display student details


void display() {
cout << "Roll No: " << rollNo << ", Name: " << name << endl;
}
};

int main() {
Student s1;

// Set values using setters


s1.setRollNo(101);
s1.setName("Alice");

// Display student details


s1.display();

// Get individual values using getters


cout << "Accessing individual details:" << endl;
cout << "Roll No: " << s1.getRollNo() << endl;
cout << "Name: " << s1.getName() << endl;

return 0;
}
8. List the types of Inheritance. Explain with examples.

1. Single Inheritance

In single inheritance, a derived class inherits from only one base class.

Syntax:
class Base {
// Base class members
};

class Derived : public Base {


// Derived class members
};

Example:
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};

class Dog : public Animal {


public:
void bark() {
cout << "Dog barks." << endl;
}
};

int main() {
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
return 0;
}

2. Multiple Inheritance

In multiple inheritance, a derived class inherits from more than one base class.

Syntax:
class Base1 {
// Base class 1 members
};
class Base2 {
// Base class 2 members
};

class Derived : public Base1, public Base2 {


// Derived class members
};

Example:
#include <iostream>
using namespace std;

class Father {
public:
void height() {
cout << "Height inherited from Father." << endl;
}
};

class Mother {
public:
void eyeColor() {
cout << "Eye color inherited from Mother." << endl;
}
};

class Child : public Father, public Mother {


public:
void talent() {
cout << "Child has unique talent." << endl;
}
};

int main() {
Child c;
c.height(); // Inherited from Father
c.eyeColor(); // Inherited from Mother
c.talent(); // Defined in Child
return 0;
}

3. Multilevel Inheritance

In multilevel inheritance, a derived class inherits from a base class, and then another class
inherits from the derived class.

Syntax:
class Base {
// Base class members
};

class Intermediate : public Base {


// Intermediate class members
};

class Derived : public Intermediate {


// Derived class members
};

Example:
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};

class Mammal : public Animal {


public:
void walk() {
cout << "This mammal walks." << endl;
}
};

class Dog : public Mammal {


public:
void bark() {
cout << "Dog barks." << endl;
}
};

int main() {
Dog d;
d.eat(); // Inherited from Animal
d.walk(); // Inherited from Mammal
d.bark(); // Defined in Dog
return 0;
}

4. Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class.

Syntax:
class Base {
// Base class members
};

class Derived1 : public Base {


// Derived class 1 members
};
class Derived2 : public Base {
// Derived class 2 members
};

Example:
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};

class Dog : public Animal {


public:
void bark() {
cout << "Dog barks." << endl;
}
};

class Cat : public Animal {


public:
void meow() {
cout << "Cat meows." << endl;
}
};

int main() {
Dog d;
Cat c;
d.eat();
d.bark();
c.eat();
c.meow();
return 0;
}

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. It often leads to the
diamond problem, where ambiguity arises if a derived class inherits the same base class
through multiple paths. This is resolved in C++ using virtual inheritance.

Example:
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};

class Mammal : virtual public Animal {


// Mammal inherits Animal
};

class Bird : virtual public Animal {


// Bird inherits Animal
};

class Bat : public Mammal, public Bird {


// Bat inherits both Mammal and Bird
};

int main() {
Bat b;
b.eat(); // No ambiguity due to virtual inheritance
return 0;
}

Summary Table:
Type of Inheritance Description Example
Single Inheritance One derived class inherits from one base class. Animal → Dog
Multiple Inheritance One derived class inherits from multiple base classes. Father, Mother → Child
Multilevel Inheritance A chain of inheritance. Animal → Mammal → Dog
Hierarchical Inheritance Multiple derived classes inherit from one base class. Animal → Dog, Cat
Hybrid Inheritance Combination of two or more inheritance types. Animal → Mammal, Bird → Bat

9. What is the purpose of Scope Resolution Operator?


10. What is Abstract class?
An abstract class in C++ is a class that cannot be instantiated directly and is used as a base class for other
classes. It contains at least one pure virtual function (a function that is declared but not defined in the
abstract class). The derived class must override these pure virtual functions.
11. List different types of polymorphism?
12. Differentiate between class and structure.

Aspect Class Structure


A class is a user-defined data type that
A structure is a user-defined data type that
Definition represents objects with data (attributes) and
primarily groups related data together.
behavior (methods).
Access Specifiers By default, members of a class are private. By default, members of a structure are public.
Structures generally do not support inheritance
Classes support inheritance, allowing one
Inheritance (in C++) or limited inheritance (in some
class to derive from another.
languages like C#).
Classes are typically used to implement Structures focus more on grouping related data,
Encapsulation
encapsulation by combining data and methods. with limited encapsulation.
Memory Objects of a class are typically allocated on the Instances of structures are usually allocated on
Allocation heap (when created dynamically). the stack (in most cases).
Structures in C++ can also have constructors,
Default Classes can have default, parameterized, and
but in C (C89), they cannot have constructors or
Constructor copy constructors, as well as destructors.
destructors.
Classes support polymorphism (e.g., virtual Structures do not support polymorphism in most
Polymorphism
functions). languages.
Used for representing complex entities with Used for grouping simpler, related data without
Use Case
data and associated behaviors. complex functionality.
Keyword Declared using the class keyword. Declared using the struct keyword.
Available in C, C++, and other structured
Support in Fully supported in C++ and most object-
programming languages, but with different
Languages oriented programming languages.
feature sets.

Example:
Class
class Car {
public:
string brand;
int year;

void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};

Structure
struct Car {
string brand;
int year;
void display() { // C++ struct can have methods.
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};

You might also like