0% found this document useful (0 votes)
4 views42 pages

OOPS2

C++ is a general-purpose object-oriented programming language developed in the early 1980s. It features classes, inheritance, function overloading, and various types of constructors and destructors. The document also discusses concepts like containership, virtual functions, and the differences between C and C++ structures.
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 views42 pages

OOPS2

C++ is a general-purpose object-oriented programming language developed in the early 1980s. It features classes, inheritance, function overloading, and various types of constructors and destructors. The document also discusses concepts like containership, virtual functions, and the differences between C and C++ structures.
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

Q1: What is C++?

Ans: C++ is a general purpose object oriented programming language invented in the early 1980

by bajarne stroutrup.

Q2: What is class?

Ans: A class can be declared as a collection of data members along with members function

which allows association of data and functions into a single unit called encapsulation.

C++ Language

Q3: What are the different features of c++?

Ans: Following are the different features of the Classes in C++,

Operators and function overloading

Free storage management

Constant types

References

Inline function

Virtual function
Templates

Exception handling

Q4: Explain constructor?

Ans: Constructors is a special member function having the same name as that of its class and is

executed automatically when the class is instantiated(object is created).

Q5: What is a function?

Ans: A function is a block of code which executes the statements when we call it.

It consists of three entities:

1) the function [Link] is simply a unique identifier.

2) The function [Link] is a set of zero or more typed identifier.

3) The function return type this specifies the type of value function returns.

Q6: Explain Inline function?

Ans: Inline function are those function whose function body is inserted in place of the function

call.

Q7: What is function overloading?

Ans: function polymorphism for function overloading is a concept that allows multiple function

to share the same name with different arguments type assigning one or more function body to

the same name is known as function overloading.


Q8: What is implicit and explicit type conversion?

Ans: In implicit casting in c++ compiler automatically handle the type [Link] final result

expressed in the highest precision possible.

Explicit: the conversion of data type of two operands is not automatic but [Link] can force

an expression to be a specific type by using a cast.

Q9: What is inheritance in C++ and name the different types of inheritance?

Ans: it is a technique of organizing information in a hierarchy form. It is like a child inheriting the

features of its parent.

The class which we are inheriting from is called as the base class and the class which inherits

called as derived class.

Different types of inheritance are as follows-:

1) single level

2) Multi-level

3) Multiple

4) Hierarchical

5) Hybrid

Q10: What is friend function?

Ans: A friend function is a function which is used to access the private data member of different

class.
Q11:

C Structure C++ Structure

Structures in C, cannot have member functions Structures in C++ can hold member functions

inside structures. with member variables.

We cannot initialize the structure data directly We can directly initialize structure data in

in C. C++.

In C, we have to write ‘struct’ keyword to In C++, we do not need to use ‘struct’

declare structure type variables. keyword for declaring variables.

C structures cannot have static members. C++ structures can have static members.

The sizeof operator will generate 0 for empty The sizeof operator will generate 1 for empty

structure in C structure in C++

The data hiding feature is not available in C The data hiding feature is present in C++

structures. structures.

C structures does not have access modifiers. C++ structures have access specifiers.
Q12: Why constructors can not be virtual in c++?

Virtual functions are used to implement polymorphic behaviour. It is used to call a function

based on the type of object pointed by the pointer variable instead of the type of the pointer

variable. Thus, “VIRTUAL” keyword allows us to call functions based on partial information only

whereas to create an object of given data type you need to know exact information of what you

want to create. Virtual tells the compiler that the type of the object used for calling the function

should be determined at run-time, but for creating an object we need to know the type at

compile type itself.

A constructor is not just an ordinary function. It is specifically coupled with the class and it’s sole

purpose is related to the creation of object or we can say that it needs to put objects data on

memory, thus it needs to the know the exact information about the Class or object and it’s

memory map. Whereas ‘virtual’ work with partial information.

What is Static Variables?

Defined with in the function, static variable initialized only once. Contents of the

variables retained throughout the program.

Define Constructor.

It is a member function having name of its class. It is executed automatically when

object is created. It is used to initialize object and allocate the necessary memory.

23. Define Destructor.


It is a member function having the char ~ followed by name of its class. It is executed

automatically when object goes out of scope. A class must have only one constructor.

24. Define Constructor Overloading.

A class can have multiple constructors. This is called constructor overloading.

25. What is order of Constructor and Destructor

When more than one object is created, they are destroyed in the reverse

Chronological order. Object created must recently is the first one to be destroyed.

26. What is meant by Parameterized constructors.

Constructor that can take arguments are called parameterized constructor.

27. What is meant by Copy Constructors?

It is used to declare and initialize an object from another object

For example

Integer i2 (i1)

Define I2 and at the same time initialize it to the values of i1.

UNIT - II
1. Define Friend Function.

Private members cannot be accessed from outside the class. To make an outside

function “ Friendly” to a class, declare this function as a friend of the class.

Define Operator Overloading?

To define an additional task to an operator. Mechanism of giving such special

meanings to an operator is known as Operator Overloading.

5. What are the Operators of C++ that cannot be overloaded?

. , .* - class member access operator

:: - Scope Resolution Operaotr

Sizeof-Size of Operator

?:- Conditional Operator

Define Virtual Base Class.

Duplication of inherited members due to multiple paths can be avoided by making

the common base class as virtual base class.

16. Define Virtual Function?

It is used to invoke exact version of the member function. Virtual functions should

be defined in the public section of a class


17. How can you access the virtual functions.

Virtual functions have to be accessed through a pointer to the base class. It is not

accessible directly.

What is containership?

A class can contain objects of other classes. It is known as containership.

In C++, containership refers to the relationship between two classes where one class contains an

object of another class or its members. The class that contains the object is called the container

class, while the class that is contained is called the contained class.

For example, consider a container class named "Ship" and a contained class named "Container".

The Ship class can contain multiple Container objects. The relationship between the Ship and

Container classes is an example of containership.

Difference between inheritance and containership.

In C++, containership and inheritance are two different concepts. Containership refers to the

relationship between two classes where one class contains an object of another class or its

members 12. Inheritance, on the other hand, is the capability of a class to derive properties

and characteristics from another class.


Containership is used to model a “has-a” relationship between two classes, while inheritance

is used to model an “is-a” relationship between two classes .

Containership is a form of object composition, while inheritance is a form of code reuse .

Containership allows for greater flexibility in the design of classes, while inheritance can lead

to tight coupling between classes .

What are rules for virtual function.

The virtual functions must be members of some class

They can not be static members

They are accessed by object pointers

Virtual function can be friend of another class.

4. What is meant by Streams?

A stream is a sequence of bytes and serves as a source or destination for an I/O data.

There 2 types of streams

Input stream

Output stream
What is meant by Static binding.

What is meant by Dynamic binding?

Answer: In C++, binding refers to the process of associating a function call with the code that

will be executed as a result of that call. There are two types of binding in C++: static binding

(also known as early binding) and late binding (also known as dynamic binding) .

Static binding occurs when the code that will be executed as a result of a function call is

determined at compile-time. This means that the function call is bound to the code that will

be executed before the program is run . Function overloading is an example of static binding

in C++ .

Late binding occurs when the code that will be executed as a result of a function call is

determined at runtime . This means that the function call is bound to the code that will be

executed while the program is running. Virtual functions are an example of late binding in C++

.
containership example

#include <iostream>

#include <string>

class Salary {

public:

Salary(double amount) : amount_(amount) {}

private:

double amount_;

};

class Employee {

public:

Employee(std::string name, int id) : name_(name), id_(id) {}

/*

In C++, std::string is a class that represents a sequence of characters. It is part of the standard

library and provides many useful methods for working with strings.

*/
void set_salary(double amount) {

salary_ = Salary(amount);

private:

std::string name_;

int id_;

Salary salary_;

};

class Company {

public:

Company(std::string name) : name_(name) {}

void add_employee(Employee employee) {

employees_[num_employees_] = employee;

num_employees_++;
}

private:

std::string name_;

Employee employees_[100];

int num_employees_ = 0;

};

int main()

Company company("Acme Corporation");

Employee employee1("John Doe", 1234);

employee1.set_salary(50000);

company.add_employee(employee1);

Employee employee2("Jane Smith", 5678);

employee2.set_salary(60000);
company.add_employee(employee2);

return 0;

In this example, we have three classes: `Employee`, `Salary`, and `Company`. The `Employee`

class contains an instance of the `Salary` class as a member variable. The `set_salary()`

method allows us to set the salary for an employee.

The `Company` class contains an array of `Employee` objects as a member variable. The

`add_employee()` method allows us to add new employees to the company.

In the `main()` function, we create two `Employee` objects and add them to the `Company`

object using the `add_employee()` method.

You might also like