1 - Difference between C & C++.
Parameter C C++
Programming C is a structural or C is a structural as well as an object-
Paradigm procedural programming oriented programming language.
language.
History C was developed by C was developed by Bjarne
scientist Dennis Ritchie in Stroustup in 1979.
1972 at Bell Laboratories.
Approach C follows a top-down C follows the bottom-up approach.
approach
Keywords C contains 32 keywords C++ contains 63 keywords.
Data Types C supports built-in data C++ support both built-in and user-
types. defined data types.
File Extension .c is the file extension for C .cpp is the file extension for C++
programming language programming language
Header File <stdio.h> header file is <iostream.h> header file is used by
used by C language C++ language
2- What is Data Abstraction ?
Data abstraction in the context of programming refers to the concept of simplifying
complex systems or objects by focusing on their essential properties and behaviors
while hiding unnecessary details. It involves creating abstract data types with well-
defined interfaces, allowing interactions at a higher level of understanding without
revealing internal implementations.
9326050669 Click here to join our Classes now 9372072139
3-Differentiate between POP and OOP. (any four points (4M)
OOP POP
Object oriented. Structure oriented.
Program is divided into objects. Program is divided into functions.
Bottom-up approach. Top-down approach.
Inheritance property is used. Inheritance is not allowed.
It uses access specifier. It doesn’t use access specifier.
Encapsulation is used to hide the data. No data hiding.
Concept of virtual function. No virtual function.
Object functions are linked through Parts of program are linked through
message passing. parameter passing.
Adding new data and functions is easy Expanding new data and functions is
not easy.
The existing code can be reused. No code reusability.
4 - Describe any four basic concepts of OOP.
Classes and Objects:
Classes: A class is a blueprint or template that defines the structure and behavior of
objects. It encapsulates data (attributes) and methods (functions) that operate on that
data. For example, a "Car" class might have attributes like "make," "model," and
methods like "startEngine" and "accelerate."
Objects: An object is an instance of a class. It represents a real-world entity with its
specific state (attribute values) and behavior (methods). For instance, you can create
objects like "Toyota Corolla" and "Ford Mustang" from the "Car" class.
9326050669 Click here to join our Classes now 9372072139
Encapsulation: Encapsulation is the principle of bundling data and the methods that
operate on that data into a single unit, i.e., a class. The internal details of how
methods work and how data is stored are hidden from external access. Only the
public interface (methods) is exposed, promoting information hiding, and protecting
the integrity of the data. Encapsulation enhances security and modularity in code.
Inheritance: Inheritance is a mechanism that allows a new class (subclass or derived
class) to inherit attributes and methods from an existing class (superclass or base
class). The subclass can further extend or override inherited attributes and methods
to create specialized behavior. Inheritance promotes code reusability and hierarchy
in class relationships. For example, a "SUV" class can inherit from the "Car" class
and add specific methods or attributes related to SUVs.
Polymorphism: Polymorphism means "many forms" and refers to the ability of
objects of different classes to be treated as objects of a common superclass.
Polymorphism allows objects to respond differently to the same method based on
their specific implementations. It promotes flexibility and enables code to work with
different types of objects in a unified way. Two common forms of polymorphism
are method overriding (subclasses provide their own implementation of a method)
and method overloading (same method name with different parameters).
These concepts together form the foundation of Object-Oriented Programming and
provide a powerful framework for designing, creating, and maintaining complex
software systems.
5 - Enlist the applications of OOP
Object-Oriented Programming (OOP) has a wide range of applications across
various domains. Here are some notable applications of OOP:
1. Software Development: OOP is extensively used in software development to
create modular, maintainable, and scalable code. It encourages the organization of
code into reusable classes and objects, making it easier to manage complex systems.
OOP concepts like encapsulation, inheritance, and polymorphism help developers
design software that is both adaptable and efficient.
9326050669 Click here to join our Classes now 9372072139
2. Graphical User Interfaces (GUIs): OOP is well-suited for building graphical user
interfaces (GUIs) in applications. GUI components like windows, buttons, and
menus can be represented as objects. OOP's encapsulation allows GUI components
to have their own attributes and behaviors, and inheritance can be used to create
specialized components.
3. Game Development: OOP is widely used in game development due to its ability
to model game elements as objects. Game objects (characters, items, enemies) can
be represented as instances of classes, allowing for easy management of their
properties and behaviors. Inheritance can be utilized to create various types of game
objects with shared attributes.
4. Simulation and Modeling: OOP is employed in simulations and modeling to
represent real-world systems. Objects can be used to model entities in the system,
while their behaviors simulate interactions and changes over time. This is
particularly useful in fields like physics, engineering, and economics.
6 - Differentiate between structure and class
Class Structure
1. Members of a class are private by 1. Members of a structure are public by
default. default.
2. An instance of a class is called an 2. An instance of structure is called the
‘object’. ‘structure variable’.
3. Member classes/structures of a class 3. Member classes/structures of a
are private by default but not all structure are public by default.
programming languages have this
default behavior eg Java etc.
4. It is declared using the class 4. It is declared using the struct
keyword. keyword.
5. It is normally used for data 5. It is normally used for the grouping
abstraction and further inheritance. of data
9326050669 Click here to join our Classes now 9372072139
6. NULL values are possible in Class. 6. NULL values are not possible.
7 - Describe syntax of 'cin' and 'cout' with example.
cin (Input): The "cin" object is used in C++ to read input from the user or other
sources. It's associated with the standard input stream and can be used to read
different types of data.
2- cout (Output): The "cout" object is used in C++ to display output to the console
or other output streams. It's associated with the standard output stream and is used
for printing data.
9326050669 Click here to join our Classes now 9372072139
8 - Explain insertion and extraction operators.
In C++, the insertion and extraction operators (<< and >>) are used for formatted
input and output operations. They are primarily associated with the standard output
stream (cout) for displaying output and the standard input stream (cin) for reading
input. These operators make it convenient to perform I/O operations in a more
human-readable and intuitive manner.
1. Insertion Operator (<<): The insertion operator (<<) is used to output data to
the standard output stream (cout). It is used to display values of various data
types, such as integers, floats, strings, and variables, to the console or other output
streams.
9326050669 Click here to join our Classes now 9372072139
2 Extraction Operator (>>): The extraction operator (>>) is used to read data from
the standard input stream (cin). It is used to accept values from the user or other
input sources and store them in variables.
9 - .List any four features of POP.
1. Procedure-Centric Approach:
In Procedural Programming, the main focus is on procedures or functions. The
program is organized into a sequence of procedures that perform specific tasks. Each
procedure can take input, process it, and produce output. This approach promotes
modularity and reusability by breaking down the program into smaller, manageable
chunks of code.
2. Global Data:
POP often relies on global data that can be accessed by any procedure in the
program. Data is not encapsulated within objects, leading to potential issues with
data integrity and security. Changes made to global data in one part of the program
can affect other parts, making it harder to track and manage changes.
9326050669 Click here to join our Classes now 9372072139
3. Sequential Execution:
In procedural programs, code execution follows a sequential flow. Procedures are
executed in the order they are called, and control flow can be managed using
constructs like loops and conditionals. While this makes the control flow easy to
understand, it can become complex in large programs.
4. Limited Code Reusability:
Code reusability in POP is generally achieved through functions or procedures.
However, the reuse is limited to functions that are explicitly called in different parts
of the program. There's no inherent mechanism for creating high-level abstractions
or relationships between different parts of the code.
5. Less Focus on Data Abstraction:
Procedural Programming places less emphasis on data abstraction and more on
procedures. While functions manipulate data, the abstraction of data and behavior
into self-contained entities (objects) isn't a central concern in POP. This can lead to
more complex code maintenance and debugging, especially in large projects.
10 - List any four object oriented languages.
1. Java: Java is a widely used, platform-independent, and versatile object-oriented
programming language. It's known for its "Write Once, Run Anywhere" capability,
which means that Java programs can run on various platforms without modification.
2. C++: C++ is an extension of the C programming language and is known for its
support of both procedural and object-oriented programming paradigms. It provides
features like classes, objects, inheritance, and polymorphism.
3. Python: Python is a versatile and easy-to-learn programming language with a
strong emphasis on readability. It supports object-oriented programming and
provides features like classes, inheritance, and encapsulation.
4. C# (C Sharp): C# is a language developed by Microsoft and is commonly used
for developing Windows applications and games using the .NET framework. It
9326050669 Click here to join our Classes now 9372072139
offers features like classes, inheritance, polymorphism, and support for graphical
user interfaces.
11 -What is the significance of scope resolution operator?
The scope resolution operator (::) is a fundamental feature in C++ (and some other
programming languages) that serves to identify and access elements, such as
variables, functions, or classes, within a specific scope. Its primary significance lies
in disambiguating and providing explicit access to elements that are defined within
different scopes.
12 - Explain structure of C++ program with example.
A C++ program has a specific structure that consists of various elements organized
in a particular way. Here's a breakdown of the typical structure of a C++ program
along with an example:
Now, let's go through each part of the C++ program structure:
9326050669 Click here to join our Classes now 9372072139
1. Preprocessor Directives: These lines start with a # symbol and are processed by
the preprocessor before actual compilation. They include header files or perform
conditional compilation.
Example: #include <iostream> - This includes the input/output stream library,
allowing you to use functionalities like cout and cin.
2. Function Declaration (Main Function): Every C++ program must have a main
function. It serves as the entry point of the program where execution begins.
Example: int main() { ... } - This declares the main function which returns an integer.
The function body is enclosed within curly braces {}.
3. Statements: Inside the main function, you write your program's logic using
statements. Statements are individual instructions that perform specific tasks.
Example: std::cout << "Hello, World!" << std::endl; - This statement uses the cout
(character output) stream to display "Hello, World!" on the console. The << operator
is used to concatenate values into the output stream.
4. Return Statement: The main function usually ends with a return statement, which
indicates the status of program execution to the operating system. Conventionally, a
return value of 0 indicates successful execution, while non-zero values indicate
errors.
Example: return 0; - This statement indicates that
13 - What are the features of procedure oriented programming
Procedure-Oriented Programming (POP) is a programming paradigm that centers
around the concept of breaking down a program into a collection of procedures or
functions. These procedures are designed to perform specific tasks and are executed
9326050669 Click here to join our Classes now 9372072139
in a sequential manner. Here are some of the key features of Procedure-Oriented
Programming:
1. Modularity: POP encourages the decomposition of a program into smaller,
manageable modules called procedures. Each procedure handles a specific task,
which promotes code reusability and maintainability.
2. Functions: The primary units of computation in POP are functions or procedures.
Each function encapsulates a set of instructions to perform a particular task.
Functions can take input parameters and return values.
3. Top-Down Design: The design process in POP often starts with a high-level view
of the problem, which is gradually broken down into smaller sub-problems and
further into procedures. This top-down approach helps in tackling complex problems
step by step.
4. Sequential Execution: Programs in POP are executed sequentially, starting from
the main function. The order of function calls determines the flow of program
execution.
5. Data Focus: While data is present in POP, the paradigm doesn't inherently provide
mechanisms for managing data and behavior together. Data can be shared among
functions, which can sometimes lead to data integrity and security issues.
14 - Define a structure with it's syntax
A structure is a composite data type that groups together variables of different data
types under a single name. It's often used to represent a record or a complex data
entity.
9326050669 Click here to join our Classes now 9372072139
15 - Explain: (i}Scope resolution operator (ii)Memory management operator
(i) Scope Resolution Operator:
The scope resolution operator is used in programming languages to access members
(variables, functions, or classes) that are defined within a specific scope, such as
classes or namespaces. It's denoted by two colons ::. This operator allows you to
explicitly specify the scope in which the member you want to access resides.
Example (C++):
(ii) Memory Management Operator:
Memory management operators are used to allocate and deallocate memory in
programming languages, typically in languages that offer manual memory
management (unlike languages with automatic garbage collection). These operators
9326050669 Click here to join our Classes now 9372072139
allow you to control how memory is allocated and released, which is important for
managing resources efficiently and avoiding memory leaks.
Example (C++):
16 - List two memory management operators available in C++ and state its use
in one line.
In C++, two memory management operators are:
1. **new**: Used to dynamically allocate memory for an object on the heap.
Example: `int *ptr = new int;`
2. **delete**: Used to deallocate memory that was previously allocated using 'new'.
Example: `delete ptr;`
17 - In procedure oriented programming all data are shared by all functions
Is this statement TRUE ?'Justify your answer
The statement "In procedural-oriented programming, all data is shared by all
functions" is not entirely true. In procedural-oriented programming (POP), data can
9326050669 Click here to join our Classes now 9372072139
be both shared and localized based on how it's declared and used within functions.
Let's explore this further:
Shared Data: In POP, global variables are accessible by all functions within the
program. These variables are shared because any function can read from or modify
their values. This can lead to potential issues like unintended changes to data due
to lack of encapsulation and control.
18 - Write a program to display largest element from entered array.
9326050669 Click here to join our Classes now 9372072139
19 - .Explain different operator used in C++.
Operators in C++ are symbols that represent operations to be performed on one or
more operands. They enable you to manipulate values and variables in various
ways. C++ provides a wide range of operators categorized into several groups:
1. Arithmetic Operators: + Addition
•- Subtraction
• * Multiplication
• / Division
• % Modulus (remainder)
2. Assignment Operators: = Simple assignment
• += Addition assignment
• -= Subtraction assignment
• *= Multiplication assignment
• /= Division assignment
9326050669 Click here to join our Classes now 9372072139
• %= Modulus assignment
• &= Bitwise AND assignment
• |= Bitwise OR assignment
• ^= Bitwise XOR assignment
• <<= Left shift assignment
• >>= Right shift assignment
3. Comparison Operators: == Equal to
• != Not equal to
• < Less than
• > Greater than
• <= Less than or equal to
• >= Greater than or equal to
4. Logical Operators:
• && Logical AND
• || Logical OR
• ! Logical NOT
5 Bitwise Operators:
• & Bitwise AND
• | Bitwise OR
• ^ Bitwise XOR
9326050669 Click here to join our Classes now 9372072139
• ~ Bitwise NOT
• << Left shift
• >> Right shift
20 - Which are the input-output C++ ? Give suitable example
In C++, the most commonly used input-output operations are performed using the
cin and cout streams, which are part of the <iostream> header. Here's a brief
explanation and an example for each:
1. cin (Input): cin is used to read input from the user or from a file. It is typically
used to read values of variables.
Example:
In this example, the program prompts the user to enter a number, reads the number
using cin, and then displays the entered number using cout. UNIT 1
9326050669 Click here to join our Classes now 9372072139
2 cout (Output): cout is used to display output to the console or to a file. It is used to
display values of variables, strings, and other data.
Example:
21 - .Write a program to display largest element from entered array.
9326050669 Click here to join our Classes now 9372072139
1 - State the characteristics of destructor
9326050669 Click here to join our Classes now 9372072139
A destructor is a special member function in object-oriented programming
languages, such as C++ or Python, that is used to clean up resources and
perform necessary cleanup operations when an object goes out of scope
or is explicitly deleted. Here are the characteristics of a destructor:
1. Name and Syntax: In C++, the destructor is named the same as the class
with a tilde (~) symbol preceding it. For example, if the class is named
MyClass, the destructor would be ~MyClass(). In Python, the destructor
is defined using the __del__() method within a class.
2. No Return Type or Parameters: Destructors do not have return types
and do not accept any parameters. They are automatically invoked when
an object's scope ends or when the delete operator is used in C++ to
explicitly destroy an object.
3. Automatic Invocation: Destructors are invoked automatically when an
object goes out of scope or is explicitly deleted. This ensures that any
cleanup operations are performed before the memory occupied by the
object is released.
4. Resource Cleanup: The primary purpose of a destructor is to release
any resources acquired by the object during its lifetime. This can include
freeing memory, closing files, releasing network connections, or any other
clean-up operations that are required for the object to be properly disposed
of.
2 - Explain memory allocation for objects
Memory allocation for objects refers to the process of reserving a portion
of the computer's memory to hold the data and state associated with an
object created in a programming language. Objects are instances of
classes in object-oriented programming, and they encapsulate both data
9326050669 Click here to join our Classes now 9372072139
(attributes) and methods (functions) that operate on that data. The memory
allocated for an object holds the values of its attributes and provides a
space for executing its methods.
The process of memory allocation for objects involves the following
steps:
1. Determine Object Size: The size of an object is determined by the size
of its attributes and any additional memory overhead required by the
programming language or runtime environment. This size can be
calculated based on the data types and structures used within the class.
2. Allocate Memory: Once the size of the object is determined, the
program requests a block of memory from the computer's memory
management system. This memory is typically allocated from the heap,
which is a region of memory used for dynamic memory allocation.
3. Constructor Execution: After the memory is allocated, the constructor
of the class is called to initialize the object. The constructor sets the initial
values for the object's attributes and performs any other setup tasks
required for the object's proper functioning.
4. Object Use: Once the memory is allocated and the constructor is
executed, the object is ready for use. Its attributes can be assigned values,
and its methods can be called to perform actions on the object's data.
5. Object Lifetime: The object's lifetime is determined by its scope and
how it was allocated. If an object is created as a local variable within a
function, it will be automatically deallocated when the function exits. If
an object is dynamically allocated using mechanisms like the new
operator in C++ or malloc() function in C, it needs to be explicitly
deallocated using delete or free() respectively to avoid memory leaks.
9326050669 Click here to join our Classes now 9372072139
6. Deallocation: When an object's lifetime ends, either due to the end of
scope or explicit deallocation, the memory it occupied is released back to
the heap or memory pool. This memory can then be reused for other
allocations.
3 - Write a program to declare a class 'Journal having data members
as journalname, price & no-of-pages. Accept this data for two objects
& display the name of journal having greater price
9326050669 Click here to join our Classes now 9372072139
4 - Write a program to define a class having data members principle,
duration & rate-of-interest. Declare rate-of-interest as static member
variable. Calculate the simple interest & display it far one object.
9326050669 Click here to join our Classes now 9372072139
9326050669 Click here to join our Classes now 9372072139
5 - State any four characteristics of constructor
Constructors are an essential part of object-oriented programming
languages like Java, C++, and Python. Here are four key characteristics
of constructors:
1. Name Same as Class: Constructors are methods within a class that have
the same name as the class itself. This allows the constructor to be
automatically invoked when an object of that class is created.
2. Initialization: Constructors are primarily used for initializing the newly
created object's state or attributes. They set initial values to the instance
9326050669 Click here to join our Classes now 9372072139
variables of the object, ensuring that the object is in a valid and usable
state upon creation.
3. No Return Type: Constructors do not have an explicit return type. In
languages like Java and C++, constructors do not specify a return type
(not even 'void'). Their primary purpose is to set up the object, not to
return a value.
4. Automatic Invocation: Constructors are automatically invoked when an
object is created using the 'new' keyword. The appropriate constructor
(based on parameters) is called, and memory is allocated for the object.
Constructors ensure that necessary setup is done before the object can be
used.
6 - Explain the concept of friend function
In object-oriented programming, a friend function is a special type of
function that is not a member of a class but is allowed to access the private
and protected members of that class. This means that a friend function has
the privilege of accessing and manipulating the private and protected data
of the class as if it were a member function, even though it is not actually
a part of the class itself.
Here are some key points to understand about friend functions:
1. Access to Private Members: Normally, class members marked as
"private" can only be accessed by member functions of the same class.
Friend functions, however, can bypass this restriction and access private
and protected members of the class.
2. Declaration in Class: To declare a function as a friend of a class, you
need to declare it within the class definition. This declaration informs the
9326050669 Click here to join our Classes now 9372072139
compiler that the specified function is a friend and can access the class's
private and protected members.
3. Not a Member of the Class: A friend function is not a member of the
class it is associated with. It exists externally to the class but has special
permission to access its private parts.
4. Scoped Independently: Since friend functions are not members of the
class, they are not called using an object of the class. Instead, they can be
called just like regular functions, using their names and appropriate
arguments.
5. Use Cases: Friend functions are often used when you need to provide
external functions, like utility functions or operators, that need access to
the private or protected data of a class. For example, in C++, the stream
insertion (<<) and extraction (>>) operators are commonly defined as
friend functions to work with user-defined classes.
6. Limited Access: While friend functions can access private members,
they should be used judiciously, as they can break encapsulation and
hinder the principles of information hiding and data protection.
7 - Explain the concept of overloaded constructors in a class with
suitable example
Overloaded constructors are a feature in object-oriented programming
languages that allows a class to have multiple constructors with different
parameter lists. This enables the creation of objects using various sets of
arguments, providing flexibility and convenience to the users of the class.
Overloaded constructors allow objects to be initialized in different ways
based on the provided arguments.
9326050669 Click here to join our Classes now 9372072139
9326050669 Click here to join our Classes now 9372072139
8 - Define class with it's syntax
9326050669 Click here to join our Classes now 9372072139
9 - Write any two rules to define friend function
To define a friend function in C++ (or similar object-oriented languages),
you need to adhere to certain rules to ensure that the function is granted
the necessary access to the private and protected members of a class. Here
are two important rules for defining a friend function:
9326050669 Click here to join our Classes now 9372072139
Declaration Inside the Class: To designate a function as a friend of a class,
you need to declare the function inside the class definition. This
declaration indicates that the function is allowed to access the private or
protected members of the class. This declaration should appear within the
class's body but outside of any other member function or access specifier.
2- Friend Function Definition: After declaring the friend function inside
the class, you need to define the actual implementation of the function
outside the class. This definition should not include the friend keyword.
The friend function definition is just like any regular function definition.
10 - What is copy constructor?
A copy constructor is a special constructor in object-oriented
programming languages like C++ that is used to create a new object as a
copy of an existing object. It is particularly used for creating a new object
with the same attributes (member variables) and values as an existing
object. Copy constructors are essential for maintaining object-oriented
principles and ensuring proper object duplication.
The copy constructor takes an object of the same class as a parameter and
uses it to initialize a new object. This process involves creating a new
instance of the class and copying the values of the attributes from the
source object to the new object.
11 - Write any two characteristics of static member function
Static member functions in object-oriented programming languages like
C++ and Java have certain characteristics that differentiate them from
regular member functions. Here are two key characteristics of static
member functions:
9326050669 Click here to join our Classes now 9372072139
1. No Access to Non-Static Members: Static member functions are
associated with the class rather than with instances of the class. As a
result, they do not have access to non-static (instance) members, including
non-static member variables and non-static member functions. This is
because static functions do not have a "this" pointer pointing to a specific
object instance. They can only access other static members of the class.
2. Invoked using Class Name: Static member functions are called using
the class name, not through object instances. Since they are not bound to
object instances, you don't need an object to invoke a static member
function. Instead, you can directly use the class name followed by the
scope resolution operator (::) to call the function.
12 - How do we invoke a constructor?
C++ Constructor basic concept
1. C++ constructors are the special member function of the class which
are used to initialize the objects of that class
2. The constructor of class is automatically called immediately after the
creation of the object.
3. Name of the constructor should be exactly same as that of name of the
class.
4. C++ constructor is called only once in a lifetime of object when object
is created.
5. C++ constructors can be overloaded
6. C++ constructors does not return any value so constructor have no
return type.
9326050669 Click here to join our Classes now 9372072139
7. C++ constructor does not return even void as return type. C++
Constructor Types 1. Default Constructor 2. Parametrized Constructor 3.
Copy Constructor We will discuss different C++ constructor types in
detail –
13 - Explain how memory is allocated to an object of a class with
diagram
Memory allocation for an object of a class varies depending on whether
the object is created on the stack or the heap. Let's discuss both scenarios
with diagrams.
1. Stack Memory Allocation: When an object is created on the stack,
memory is allocated from the stack memory area. Stack memory is
organized in a last-in, first-out (LIFO) manner, and it's typically faster to
allocate and deallocate than heap memory. However, stack memory has
size limitations and is best suited for objects with a relatively small and
known lifetime.
2. Heap Memory Allocation: When an object is created on the heap,
memory is allocated from the heap memory area. Heap memory has a
larger size compared to the stack and can accommodate objects with
dynamic lifetimes. However, heap memory allocation and deallocation
can be slower than stack memory operations.
14 - Write any four rules to define constructor in a class
1. Same Name as Class: Constructors must have the same name as the
class they belong to. This naming convention ensures that the constructor
is automatically called when an object of the class is created.
9326050669 Click here to join our Classes now 9372072139
2. No Return Type: Constructors do not have a return type, not even void.
Their primary purpose is to initialize the object's state, and they cannot
return a value.
3. Overloading Constructors: You can define multiple constructors in a
class, differing in the number or types of parameters they accept. This is
known as constructor overloading and allows for different ways to
initialize objects of the class.
4. Initialization of Members: Constructors are used to initialize the
member variables (attributes) of the object being created. You should use
the constructor's parameters to set initial values for the members based on
the provided arguments.
15 - Write a program to define a class student having data members
name and roll no. Accept and display data for one object
9326050669 Click here to join our Classes now 9372072139
16 - What is destructor ? Give its syntax. How many destructors can
be defined in a single class ?
A destructor is used to destroy the memory allocated by the constructor.
The following points are to be noted about destructor. 1. It is defined only
in the public visibility of a class. 2. The name of destructor must be same
as that of class prefix with a tidle (~) sign. The name of the constructor
and destructor are same as the class. Hence to differentiate between the
two, the tidle sign is used. 3. It cannot return or accept any value. 4. It is
automatically called when the compiler exits from the main program. 5.
Syntax: ~ClassName() { } C++ Destructor programs
9326050669 Click here to join our Classes now 9372072139
17 - How many ways we can define member function in class ? Give
it's syntax
Member functions can be defined within the class definition or separately
using scope resolution operator, ::. Defining a member function within the
class definition declares the function inline, even if you do not use the
inline specifier. So either you can define Volume() function as below
9326050669 Click here to join our Classes now 9372072139
18 - Explain constructor with default argument
If the programmer does not specify the constructor in the program then
compiler provides the default constructor.
2. In C++ we can overload the default compiler generated constructor 3.
In both cases (user created default constructor or default constructor
generated by compiler), the default constructor is always parameter less.
9326050669 Click here to join our Classes now 9372072139
19 - State any two access specifier with example
When deriving a class from a base class, the base class may be inherited
through public, protected or private inheritance. The type of inheritance
is specified by the access-specifier as explained above. We hardly use
9326050669 Click here to join our Classes now 9372072139
protected or private inheritance, but public inheritance is commonly used.
While using different type of inheritance, following rules are applied:
•Public Inheritance: When deriving a class from a public base class, public
members of the base class become public members of the derived class
and protected members of the base class become protected members of
the derived class. A base class's private members are never accessible
directly from a derived class, but can be accessed through calls to the
public and protected members of the base class. •Protected Inheritance:
When deriving from a protected base class, public and protected members
of the base class become protected members of the derived class. •Private
Inheritance: When deriving from a private base class, public and protected
members of the base class become private members of the derived class.
20 - Define constructor overloading
In C++, We can have more than one constructor in a class with same
name, as long as each has a different list of arguments.This concept is
known as Constructor Overloading and is quite similar to function
overloading
Overloaded constructors essentially have the same name (exact name
of the class) and different by number and type of arguments.
A constructor is called depending upon the number and type of
arguments passed.
While creating the object, arguments must be passed to let compiler
know, which constructor needs to be called.
21 - List any four properties of constructor function
• Constructor is a member function of a class, whose name is same as the
class name. • Constructor is a special type of member function that is used
9326050669 Click here to join our Classes now 9372072139
to initialize the data members for an object of a class automatically, when
an object of the same class is created. • Constructor is invoked at the time
of object creation. It constructs the values i.e. provides data for the object
that is why it is known as constructor. • Constructor do not return value,
hence they do not have a return type.
22- List four types of constructors
1- Default constructor: It is a constructor that is automatically provided
by the programming language when no explicit constructor is defined in
a class. It initializes the object's variables with default values.
2 - Parameterized constructor: It is a constructor that accepts parameters
or arguments during object creation. It allows the programmer to initialize
the object's variables with specific values supplied at the time of
instantiation.
3 - Copy constructor: It is a constructor that allows creating a new object
by copying the values of an existing object. It initializes a new object with
the same values as another object.
4 - Private constructor: It is a constructor that is declared as private,
restricting its access to the class itself. This type of constructor is
commonly used in singleton design patterns, where only one instance of
the class is allowed to be created.
23 - Differentiate between constructor and destructor
S. No. Constructor Destructor
9326050669 Click here to join our Classes now 9372072139
1. Constructor helps to initialize Whereas destructor is
the object of a class. used to destroy the
instances.
2. It is declared as className( Whereas it is declared
arguments if any as ~ className( no
){Constructor’s Body }. arguments ){ }.
3. Constructor can either accept While it can’t have
arguments or not. any arguments.
4. A constructor is called when an It is called while
instance or object of a class is object of the class is
created. freed or deleted.
5. Constructor is used to allocate While it is used to
the memory to an instance or deallocate the
object. memory of an object
of a class.
6. Constructor can be overloaded. While it can’t be
overloaded.
7. The constructor’s name is same Here, its name is also
as the class name. same as the class
name preceded by the
tiled (~) operator.
24 - Explain copy constructor with example
1. All member values of one object can be assigned to the other object
using copy constructor.
2. For copying the object values, both objects must belong to same class.
Syntax
9326050669 Click here to join our Classes now 9372072139
9326050669 Click here to join our Classes now 9372072139