C++ Very short answer
Chapter 3
1. What is class? Explain with an
example.
A class is
a blueprint to create objects. It contains variables and functions related to
the object. Example:
class Car {
int speed; void drive(); };
2. How do you declare a class in C++? Explain with an
example.
Declare a
class using the keyword class followed by the name and body in braces. Example:
class
Student { int id; void show(); };
3. What is a constructor in C++? How do you define a
constructor in C++?
A constructor is a special function with the same name as the
class. It runs automatically when an object is created. Example: class Demo {
Demo() { } };
4. Can a constructor be called explicitly in C++?
Explain.
Yes, constructors can be called explicitly using syntax like
ClassName obj = ClassName();. Usually, constructors are called automatically
when objects are created in C++.
5. Explain this pointer in C++. this pointer is an implicit pointer
inside class functions. It points to the current object calling the function.
It helps access class members when names are similar.
6. What is a friend class in C++?
A friend class can access private and protected members of
another class. It is declared using friend class keyword inside the original
class. Useful for close class relations.
7. What is a friend function in C++?
A friend function is not a member but can access private and
protected data. Declared inside a class using the keyword friend. Useful for
external access to private members.
8. How do you declare a friend function in C++?
Inside a
class, declare a friend function with the keyword friend. Example:
class A {
friend void display(A); };
Define the function outside the class normally.
9. What is a static member of a class in C++? How do you
define it?
Static members belong to the class, not objects. Declare
inside class with static, then define outside like: int ClassName::var = 0;.
Shared among all objects.
10. How do you access a static member of a class in C++?
Access
static members using the class name without objects. For example:
ClassName::member.
Objects are not needed to access static members or modify
their values.
Unit 3 C++
Short
answer
1. What is the difference between a class and object in C++?
In C++, a
class is a blueprint or template for
creating objects. It defines the data
members (variables) and member
functions (methods) that represent the properties and behaviors of an
entity. Think of a class as a design plan. On the other hand, an object is an instance of a class; it is
a real entity created based on the class blueprint. For example, if Car is a
class, then myCar and yourCar are objects of that class. Each object has its
own copy of data members, though they share the same functions defined in the
class. Classes are abstract definitions,
while objects are concrete
implementations. Classes allow encapsulation, inheritance, and polymorphism
in C++, which are key principles of objectoriented programming. Objects make
these features usable in programs. Without objects, classes would just be
definitions with no practical use.
2. How do you define member functions for a class in C++?
Member
functions of a class are functions that operate on the data members of that class. They are usually defined inside the class definition or outside using the scope resolution operator
(::). For example, if a class Student has a function display(), you can
define it inside the class like void display() { /* code */ }. To define it
outside, you write void Student::display() { /* code */ }. Member functions can
access private and protected members directly, but external functions cannot.
They allow data encapsulation,
meaning the internal representation is hidden, and operations are controlled.
Member functions can be inline, virtual,
static, or regular depending on the requirement. Proper definition and use
of member functions are essential for modular and maintainable C++ code.
3.
How do you access the members of a class in C++? In C++, the members of a class are accessed using objects of that class. The syntax is
objectName.memberName; for data members or member functions. For example, if
Student s; is an object of class Student and name is a member variable, we
access it as s.name. Private members cannot be accessed directly outside the
class; instead, we use public member
functions (getters/setters) to manipulate them. Alternatively, pointers to objects can also be used
with -> operator. Accessing members correctly ensures data security and proper encapsulation, which is a major principle
of objectoriented programming in C++.
4. What is the difference between a default constructor and a
parameterized constructor in C++?
A default
constructor is a constructor that does not take any parameters. It
initializes objects with default values. For example, Student() {} is a default
constructor. A parameterized constructor
takes arguments to initialize an object with specific values, e.g.,
Student(string n, int a) { name = n; age = a; }. Default constructors are
called automatically when an object is created without arguments, whereas
parameterized constructors require arguments during object creation. Using
parameterized constructors allows more flexibility in object initialization.
Both are special member functions that help in automatic initialization of objects, reducing the need for separate
initialization functions.
5. What is the
difference between a constructor and a destructor in C++?
A constructor
is a special member function used to initialize
objects of a class automatically when they are created. It has the same
name as the class and does not return any value. A destructor, on the other hand, is used to destroy objects and release resources before the object is removed
from memory. It has the same name as the class but is preceded by a tilde (~)
and also returns no value. Constructors are called when an object is created,
destructors are called automatically when an object goes out of scope.
Constructors can be overloaded, while destructors
cannot.
Both ensure proper resource management and object lifecycle control in C++.
6. Explain various access
specifiers in C++. C++ provides three
access specifiers:
•
Public: Members declared public can be accessed from anywhere in
the program.
•
Private: Members declared private can only be accessed within the
class; they are hidden from outside functions or objects.
•
Protected: Members declared protected can be accessed within the
class and its derived classes, but
not outside.
Access specifiers control visibility and encapsulation of data. Choosing the correct
specifier ensures that sensitive data is protected and that the class interface
is used correctly. By default, class members are private, and struct members
are public. Proper use of access specifiers is a cornerstone of object-oriented programming security and
modularity.
7. What is the purpose of a
friend class in C++?
A friend
class in C++ is a class whose members can access the private and protected members of another class in which it
is declared a friend. This allows tighter cooperation between two classes
where one class needs to operate directly on the internal data of another
class. Friendship is not mutual
unless explicitly declared. Friend classes do not violate encapsulation because
access is given intentionally, but it should be used carefully to avoid
unnecessary dependencies. They are commonly used when two classes are closely
related and need to work together, such as in operator overloading or complex data structures.
8. What is the
difference between friend class function and a member function in C++?
A member
function is part of the class and can access all private, protected, and
public members of that class. A friend
function is not part of the class,
but it is declared as a friend to the class to access private and protected
members. Unlike member functions, friend functions do not have a this pointer, and they must be called like normal
functions, not via an object. Friend functions provide controlled access without making members public. They are useful in
operator overloading and when multiple classes need to interact closely.
9. How is a
static member different from a non-static member of a class in C++?
A static
member belongs to the class itself, not to any particular object. It is
shared among all objects of the class, and memory is allocated only once. A non-static member belongs to a specific
object, and each object has its own copy. Static members can be accessed using
the class name (ClassName::staticMember) without creating an object. Nonstatic
members require an object for access. Static members are commonly used for shared counters or constants, whereas
non-static members store object-specific
data.
0 Comments