KEMBAR78
BPLCK205D Assignment Answers | PDF | Message Passing | Object Oriented Programming
0% found this document useful (0 votes)
99 views4 pages

BPLCK205D Assignment Answers

The document discusses key concepts in C++ programming, including message passing, abstract classes, keywords and identifiers, special assignment expressions, constructors, and destructors. It provides examples for each concept to illustrate their functionality and importance in object-oriented programming. The document serves as a guide for understanding these fundamental programming principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views4 pages

BPLCK205D Assignment Answers

The document discusses key concepts in C++ programming, including message passing, abstract classes, keywords and identifiers, special assignment expressions, constructors, and destructors. It provides examples for each concept to illustrate their functionality and importance in object-oriented programming. The document serves as a guide for understanding these fundamental programming principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment Test-I Answers (New Copy)

Question 1: Explain Message Passing with an example. (10 Marks)


Message passing is a key concept in object-oriented programming (OOP), where objects
communicate with each other by sending and receiving information (messages). It
promotes interaction between objects while encapsulating their internal data.

Each object has methods (functions) and data. When an object wants to request a service or
action from another object, it sends a message in the form of a method call.

Example:
#include <iostream>
using namespace std;

class Student {
public:
void displayMessage() {
cout << "Message received: Hello from Student class!" << endl;
}
};

int main() {
Student s1;
s1.displayMessage(); // Message passing
return 0;
}
In this example, the object `s1` sends a message to itself by calling `displayMessage()`. This
demonstrates the message passing concept in C++.

Question 2: What are abstract classes? Discuss with an example. (10


Marks)
An abstract class in C++ is a class that cannot be instantiated and serves as a base class. It
contains at least one pure virtual function, which is a function declared with `= 0`.

Example:
#include <iostream>
using namespace std;

class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {


public:
void draw() {
cout << "Drawing Circle" << endl;
}
};

int main() {
Circle c;
c.draw();
return 0;
}
In this example, `Shape` is an abstract class because of the pure virtual function `draw()`.
`Circle` inherits from `Shape` and implements `draw()`. This enforces a standard interface
and enables polymorphism.

Question 3: Define the term keywords and identifiers and also write the
rules for defining identifiers. (10 Marks)
Keywords are reserved words predefined by the C++ language that have special meaning
and cannot be used as identifiers. Examples: `int`, `class`, `return`.

Identifiers are names used to identify variables, functions, arrays, etc.

Rules for defining identifiers:


1. Must begin with a letter (A-Z, a-z) or underscore (_).
2. Cannot start with a digit.
3. Cannot use reserved keywords.
4. Case-sensitive.
5. Cannot contain special characters or spaces.

Example:
int number1; // Valid
float _value; // Valid
char 3rdChar; // Invalid (starts with digit)
int class; // Invalid (keyword)
Question 4: Explain 3 types of special assignment expressions and scope
resolution operator with suitable examples. (10 Marks)
Special Assignment Expressions:
1. Chained Assignment:
int a, b, c;
a = b = c = 10;

2. Compound Assignment:
int x = 5;
x += 3; // x = x + 3

3. Unary Assignment (Increment/Decrement):


int i = 5;
i++; // Post-increment
++i; // Pre-increment

Scope Resolution Operator (`::`):


Used to access global variables or define functions outside class scope.
#include <iostream>
using namespace std;

int num = 100;

int main() {
int num = 50;
cout << "Local num: " << num << endl;
cout << "Global num: " << ::num << endl;
return 0;
}

Question 5: With an example program, discuss Constructors. (5 Marks)


A constructor is a special member function that is automatically invoked when an object is
created.

Example:
#include <iostream>
using namespace std;

class Student {
public:
Student() {
cout << "Constructor called. Object created!" << endl;
}
};

int main() {
Student s1;
return 0;
}

Question 6: With an example program, discuss Destructors. (5 Marks)


A destructor is a special member function that is automatically called when an object goes
out of scope or is deleted.

Example:
#include <iostream>
using namespace std;

class Student {
public:
Student() {
cout << "Constructor called!" << endl;
}

~Student() {
cout << "Destructor called!" << endl;
}
};

int main() {
Student s1;
return 0;
}

You might also like