0% found this document useful (0 votes)
0 views

C++_Concepts_Notes

Object Oriented Programming (OOP) is a programming paradigm that uses objects to bundle data and methods, promoting modularity and reusability, while Object-Based Programming lacks full OOP principles like inheritance and polymorphism. Key concepts include methods, abstract classes, functions, variables, operations, inline functions, and C++ tokens. The document provides examples in C++ to illustrate these concepts.

Uploaded by

testrest227
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C++_Concepts_Notes

Object Oriented Programming (OOP) is a programming paradigm that uses objects to bundle data and methods, promoting modularity and reusability, while Object-Based Programming lacks full OOP principles like inheritance and polymorphism. Key concepts include methods, abstract classes, functions, variables, operations, inline functions, and C++ tokens. The document provides examples in C++ to illustrate these concepts.

Uploaded by

testrest227
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4. What is Object Oriented Programming? How is it different from Object-Based Programming?

Object Oriented Programming (OOP):

OOP is a programming paradigm based on the concept of "objects", which contain data and methods to

operate on that data. It allows data and functions to be bundled together, promoting modularity, code

reusability, and organization.

Example of OOP (in C++):

class Car {

public:

string brand;

void start() {

cout << "Car started";

};

Object-Based Programming:

Object-based programming also uses objects but does not support all OOP principles like inheritance and

polymorphism. Example: JavaScript (before ES6).

Differences:

- OOP supports all principles (encapsulation, inheritance, polymorphism).

- Object-based doesn't support inheritance/polymorphism.

- C++ is OOP; JavaScript is object-based.

----------------------------------------------------------------------
Explain the following with a code snippet:

(i) Methods

(ii) Abstract classes

(i) Methods:

Functions defined inside a class are called methods.

Example:

class Car {

public:

string brand;

void display() {

cout << "Brand: " << brand;

};

(ii) Abstract Classes:

A class with at least one pure virtual function is abstract.

Example:

class Animal {

public:

virtual void sound() = 0; // Pure virtual function

};

class Dog : public Animal {

public:
void sound() {

cout << "Dog barks";

};

----------------------------------------------------------------------

What is a function? How to declare and define functions in C++? Explain with examples.

Function:

A function is a block of code that performs a specific task.

Function Declaration:

int add(int, int);

Function Definition:

int add(int a, int b) {

return a + b;

Function Call:

int result = add(5, 3);

Example:

#include <iostream>

using namespace std;


int add(int a, int b) {

return a + b;

int main() {

cout << "Sum = " << add(10, 5);

return 0;

----------------------------------------------------------------------

List and explain the different variables and operations in C++.

Explain briefly the scope resolution operator.

Variables:

- Local: Defined inside a function

- Global: Defined outside all functions

- Static: Retains value between function calls

- Constant: Value cannot change

- Reference: Another name for an existing variable

Operations:

- Arithmetic: + - * / %

- Relational: == != > < >= <=

- Logical: && || !

- Assignment: = += -=
Scope Resolution Operator ::

- Used to access global variables or define class methods outside class

Example:

int x = 10;

int main() {

int x = 20;

cout << ::x; // Outputs 10

----------------------------------------------------------------------

4. What is an inline function? How is it different from a normal function?

Inline Function:

A function where code is expanded at the point of call to save call overhead. Declared using `inline`.

Example:

inline int square(int x) {

return x * x;

Difference:

- Inline: Replaces function call with code

- Normal: Makes a function call

- Inline is faster for small functions but increases code size


----------------------------------------------------------------------

Explain C++ Tokens and Reference Variables with examples.

C++ Tokens:

1. Keywords (int, return)

2. Identifiers (x, main)

3. Literals (10, 3.14, "Hi")

4. Operators (+, -, =)

5. Punctuators (;, {}, ())

6. Comments (//, /* */)

Reference Variables:

An alias for another variable. Declared using &.

Example:

int a = 5;

int &ref = a;

ref = 10; // Now a = 10

You might also like