[go: up one dir, main page]

0% found this document useful (0 votes)
38 views22 pages

C++ Internal

Uploaded by

gaminggolgappa22
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)
38 views22 pages

C++ Internal

Uploaded by

gaminggolgappa22
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/ 22

SHORT QUESTION ANSWER

Ans 1:-
#include<iostream>
using namespace std;

class Demo{
private :
int a;
public :
int b;

void setdata(int a1)


{
a=a1;
}
void getdata()
{
cout<<"Private Variable = "<<a<<endl;
cout<<"Public Variable = "<<b<<endl;
}
protected:
float e;

public:
void prot(float e1)
{
e=e1;
}
};
class Stud : public Demo{
public:
void details()
{
cout<<"Protected variable = "<<e<<endl;
}
};
int main()
{
Demo d;
d.setdata(21);
d.b =9;
d.getdata();
Stud s;
s.prot(4.1f);
s.details();
return 0; }
Output

Ans 2:- Exception Handling in C++ is a process to handle runtime


errors. We perform exception handling so the normal flow of the
application can be maintained even after runtime errors.

In C++, exception is an event or object which is thrown at runtime. All exceptions


are derived from std::exception class. It is a runtime error which can be handled.
If we don't handle the exception, it prints exception message and terminates the
program.

Exceptions provide a way to transfer control from one part of a


program to another. C++ exception handling is built upon three
keywords: try, catch, and throw.
1. throw − A program throws an exception when a problem shows
up. This is done using a throw keyword.
2. catch − A program catches an exception with an exception
handler at the place in a program where you want to handle the
problem. The catch keyword indicates the catching of an
exception.
3. try − A try block identifies a block of code for which particular
exceptions will be activated. It's followed by one or more catch
blocks.

How Exceptions are Handling in C++:

1. The try block contains code that might generate an exception.


2. If an exception occurs, the throw keyword is used to signal the exception.
3. The program looks for a matching catch block that can handle the thrown
exception.
4. If a matching catch block is found, the exception is handled, and the
program continues after the catch block.
5. If no matching catch block is found, the program terminates or propagates
the exception further up the call stack.

Ans 3:-
A destructor is a special member function that is called automatically when an
object goes out of scope or when we delete the object with the delete
expression.
In C++, a destructor has the same name as that of the class, and it does not
have a return type. ~ precedes the identifier to indicate destructor.

Syntax to Define Destructor


The syntax for defining the destructor within the class:
~ <class-name>() {
// some instructions
}

When is the destructor called?

A destructor function is called automatically when the object goes out of scope or
is deleted. Following are the cases where destructor is called:
-> Destructor is called when the function ends.
-> Destructor is called when the program ends.
-> Destructor is called when a block containing local variables ends.
-> Destructor is called when a delete operator is called.

Example of Destructor :-
#include<iostream>
using namespace std;
class demo
{
public:
demo()
{
cout<<"I am constructor"<<endl;
}
~demo()
{
cout<<"I am destructor";
}
};
int main()
{
demo d;}
Output ->
Ans 4->

How they are usefull ->


Example :-
Ans 5 :- In C++, default arguments are values that are assigned to
function parameters when the function is called without providing
arguments for those parameters. This feature allows functions to be
called with fewer arguments than they are defined to accept. If
arguments are not provided, the default values are used automatically.

Default arguments in C++ are specified in the function declaration or


definition. These default values must be provided from right to left in
the function's parameter list. This means that if you provide a default
value for one parameter, all parameters to the right of it must also have
default values.

The rules of declaring default arguments -


• o The values passed in the default arguments are not constant.
These values can be overwritten if the value is passed to the function. If
not, the previously declared value retains.
• o During the calling of function, the values are copied from left to
right.
• o All the values that will be given default value will be on the
right.
Example :-
#include<iostream>
using namespace std;
int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default
arguments
{ // Both z and w are initialised to zero
return (x + y + z + w); // return sum of all parameter values }

int main() {
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0
cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30
return 0;
}
Output
25
50
80

Ans 6 :-

#include <iostream>
using namespace std;
class Factorial {
int number;
long long fact;
public:
// Constructor that calculates factorial
Factorial(int n) {
number = n;
fact = 1;
for (int i = 1; i <= number; i++) {
fact=fact*i;
}
}
// Method to display the factorial result
void display(){
cout << "Factorial of " << number << " is: " << fact <<
endl;
}
};
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
// Create an object of Factorial, which automatically
computes the factorial
Factorial f(num);
f.display();
return 0;
}
Output

LONG QUESTION ANSWER

Ans 1:- Inheritance is one of the fundamental principles of Object-


Oriented Programming (OOP). It allows a new class (called a derived
class) to inherit properties and behaviors (data members and member
functions) from an existing class (called a base class). This helps in
code reuse and creating a hierarchy of classes where a derived class can
extend or modify the behavior of the base class.

Inheritance enables the concept of "is-a" relationship. This means that


the derived class is a type of the base class, inheriting its characteristics
and behaviors while also being able to add or modify its own specific
attributes and behaviors.
Types of Inheritance :-
Example of Single Inheritance

Output

Program
Ans 2:- In C++, file operations are done using file stream
classes such as ifstream, ofstream, and fstream, which handle
the reading and writing of files. Let's dive into the explanation
of the specific file operations you've mentioned:
(a) open()
The open() function is used to open a file for reading, writing,
or both. When a file is opened successfully, it is associated with
a file stream object, and operations like reading or writing can
be performed on it.
Syntax:
ifstream infile; // Input file stream
ofstream outfile; // Output file stream

infile.open("input.txt", ios::in); // Open file for reading


outfile.open("output.txt", ios::out); // Open file for writing

-> Open() takes the file name as a parameter and an optional mode flag
(ios::in, ios::out, ios::app, etc.).
-> If the file cannot be opened (e.g., it doesn't exist for reading or there
are permission issues), the open() method sets the file stream object to a
"failed" state.

(b) read()

The read() function is used to read data from a file in binary mode. It is
often used when reading raw data (e.g., integers, structures, or arrays)
from a file.

Syntax:
ifstream infile;
infile.read(reinterpret_cast<char*>(&buffer), sizeof(buffer));

 read() reads a specified number of bytes from a file into a buffer.


 reinterpret_cast<char*>(&buffer) is used to treat the buffer as a
sequence of bytes (since files are read byte by byte).
 sizeof(buffer) specifies how many bytes should be read into the
buffer.
(c) close()

The close() function is used to close a file once file operations are
completed (whether reading or writing). Closing a file is essential
because it ensures that all resources (such as file handles) are released
properly and that any data is written to disk.

Syntax:
infile.close(); // Close the input file
outfile.close(); // Close the output file

 After calling close(), the file is no longer accessible until it is


opened again.
 It is a good practice to explicitly close a file after performing
operations on it, although C++ automatically closes files when the
file stream object goes out of scope.

(d) EOF

EOF, or End of File, is a condition in computing that signifies no more


data is available for reading from a file or input stream. In many
programming languages, EOF is represented by a special constant that
tells the program that it has reached the end of the input or file
content.

Syntax:
ifstream infile;
while (!infile.eof()) {
// Read data}
Ans 3:- #include<iostream>
using namespace std;
class complex{
int x,y;
public:
complex(int real,int img)
{ x=real;
y=img; }

void output()
{ cout<<"X="<<x;
cout<<"Y="<<y<<endl; }

complex operator+(complex); //Declaration of operating


function
};

complex complex::operator+ (complex ob)


{ complex t(x,y);
t.x=x+ob.x;
t.y=y+ob.y;
return(t); }

int main(){
complex ob1(10,20);
complex ob2(30,40);
complex ob3(10,30);
ob3=ob2+ob1; //valid method
ob1.output();
ob2.output();
ob3.output();
return(0);
}
Output ->>
Ans 4 :-
Example of constructor :-
Types of constructor :-
1.Default Constructor
A default constructor is a constructor that takes no parameters.
It is called automatically when an object is created without any
arguments.
 If you don't define any constructor in a class, the compiler
provides a default constructor that initializes members to
default values.
 If you define a constructor with parameters, and do not
define a default constructor, the compiler won't provide
one.
2. Parameterized Constructor
A parameterized constructor is a constructor that accepts one
or more arguments. It allows the user to initialize the object with
custom values at the time of object creation.
3. Copy Constructor
A copy constructor is used to create a new object as a copy of
an existing object. It is called when:
 A new object is created from an existing object of the same
class (e.g., passing an object by value, returning an object
by value from a function).
 The default copy constructor performs a shallow copy, but
you can define a custom copy constructor for a deep copy
(when an object contains pointers).
Ans 5 :- (A) Polymorphism
(B) Abstract Class
By definition, a C++ abstract class must include at least one pure
virtual function. Alternatively, put a function without a definition.
Because the subclass would otherwise turn into an abstract class
in and of itself, the abstract class's descendants must specify the
pure virtual function.
Broad notions are expressed using abstract classes, which can
then be utilized to construct more specific classes. You cannot
make an object of the abstract class type. However, pointers and
references can be used to abstract class types. When
developing an abstract class, define at least one pure virtual
feature. A virtual function is declared using the pure specifier (= 0)
syntax.
Consider the example of the virtual function. Although the class's
objective is to provide basic functionality for shapes, elements of
type shapes are far too general to be of much value. Because of
this, the shape is a good candidate for an abstract class:

(C)Nested Class
A nested class in C++ is a class defined within another class. The nested
class has the scope of the outer class, and it can access the members
(both private and public) of the outer class, if explicitly allowed. A
nested class is often used when the inner class is closely related to the
outer class and isn't meant to be used outside of it.

There are two main types of nested classes in C++:

1. Static Nested Class: The inner class does not have access to the
non-static members of the outer class.
2. Non-static Nested Class: The inner class has access to the non-
static members of the outer class (i.e., it behaves like a regular
member function).

Syntax:
class OuterClass {
class NestedClass {
// Nested class definition
};
};
(D) Namespace
A namespace is designed to overcome this difficulty and is used
as additional information to differentiate similar functions,
classes, variables etc. with the same name available in different
libraries. Using namespace, you can define the context in which
names are defined. In essence, a namespace defines a scope.

For example, you might be writing some code that has a


function called xyz() and there is another library available which
is also having same function xyz(). Now the compiler has no
way of knowing which version of xyz() function you are
referring to within your code. To overcome this problem we use
namespace.

(E) Templates
Ans 6 :-
#include <iostream>
#include <cmath>
using namespace std;
class AreaCalculator {
public:

double area(double length, double width) {


return length * width;
}

double area(double a, double b, double c) {


double s = (a + b + c) / 2.0;
return sqrt(s * (s - a) * (s - b) * (s - c));
}

double area(double radius) {


return 3.14 * radius * radius;
}};

int main()
{
AreaCalculator calculator;
double length = 5.0, width = 10.0;
double side1 = 3.0, side2 = 4.0, side3 = 5.0;
double radius = 7.0;

cout << "Area of Rectangle: " << calculator.area(length, width)


<<
endl;

cout << "Area of Triangle: " << calculator.area(side1, side2,


side3) <<
endl;

cout << "Area of Circle: " << calculator.area(radius) << endl;


return 0;}

Output 

You might also like