- 2068 Baisakh · 5+5 marks
C versus C++ and C++ features
Short answer
C is procedural; C++ adds object-oriented capabilities. Key C++ features: classes (class Box { int side; }), overloading (int add(...), float add(...)), templates (template<class T> T max(T,T)), inheritance, exceptions, references, and new/delete for dynamic memory.
Compare C and C++. Write down different features of C++ with example for each.
Comparison of C and C++
| Point | C | C++ |
|---|---|---|
| Paradigm | Procedural | Procedural and object oriented |
| Basic unit | Function | Class / object |
| Data hiding | Not possible | Possible (private, protected) |
| Overloading | Not supported | Function and operator overloading |
| Memory | malloc() / free() | new / delete |
| I/O | printf() / scanf() | cout / cin |
| References | No (pointers only) | Reference variables |
| Inheritance and polymorphism | No | Yes |
| Exceptions, templates, namespaces | No | Yes |
| Inline functions | Not in original C | Supported |
Features of C++ with examples
- Classes and objects
class Box { public: int side; };
Box b; b.side = 5;
- Encapsulation and data hiding
class A { private: int x; public: void set(int v) { x = v; } };
- Inheritance
class Student : public Person { };
- Function overloading
int add(int a, int b); float add(float a, float b);
- Operator overloading
Complex operator+(Complex c);
- Polymorphism by virtual functions
class Shape { public: virtual void draw(); };
- Templates
template <class T> T maxOf(T a, T b) { return a > b ? a : b; }
- Exception handling
try { if (d == 0) throw 0; } catch (int e) { cout << "Divide by zero"; }
- Reference variables and inline functions
void swap(int &a, int &b); inline int sq(int x) { return x * x; }
- Dynamic memory
int *p = new int[10]; delete[] p;
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Data abstraction versus encapsulation · Object Oriented Programming, chapter 1
- Browser features; POP, SMTP, IMAP comparison · Internet and Intranet, chapter 3 · asked 2×
- MBI features; open vs closed loop compared · Instrumentation II, chapter 1
- Macadam versus Telford construction · Transportation Engineering I, chapter 1 · asked 2×
- UMS features, VoIP in UMS, IP clearing house · Internet and Intranet, chapter 7