0% found this document useful (0 votes)
1 views7 pages

C++ Session1 15 June 2025

The document provides an overview of Object-Oriented Programming (OOP) concepts in C++, including objects, classes, encapsulation, inheritance, polymorphism, dynamic binding, and message passing. It also covers variable naming rules, data types, constants, and examples of using printf and scanf in C++. Additionally, it explains the use of comments and identifiers in C++ programming.

Uploaded by

hardik140692
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)
1 views7 pages

C++ Session1 15 June 2025

The document provides an overview of Object-Oriented Programming (OOP) concepts in C++, including objects, classes, encapsulation, inheritance, polymorphism, dynamic binding, and message passing. It also covers variable naming rules, data types, constants, and examples of using printf and scanf in C++. Additionally, it explains the use of comments and identifiers in C++ programming.

Uploaded by

hardik140692
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/ 7

Object-Oriented Programming (OOP) in C++ is an approach where programs are structured

around objects rather than functions and logic. Key OOP concepts in C++ include:

1. Objects: Basic run-time entities that are instances of a class.

2. Class: A blueprint of data and functions.

#include <iostream>

#include <string>

using namespace std;

class MyClass { // The class

public: // Access specifier

int myNum; // Attribute (int variable)

string myString; // Attribute (string variable)

};

int main() {

MyClass myObj; // Create an object of MyClass

// Access attributes and set values

myObj.myNum = 15;

myObj.myString = "Some text";

// Print values

cout << myObj.myNum << "\n";

cout << myObj.myString;

return 0;

}
3. Encapsulation and Data Abstraction: Combining data and functions into a single unit.

class MyClass { // The class


public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};

4. Inheritance: Mechanism for creating a new class from an existing class.

#include <iostream>

#include <string>

using namespace std;

// Base class

class Vehicle {

public:

string brand = "Ford";

void honk() {

cout << "Tuut, tuut! \n" ;

};

// Derived class

class Car: public Vehicle {

public:

string model = "Mustang";

};
int main() {

Car myCar;

myCar.honk();

cout << myCar.brand + " " + myCar.model;

return 0;

5. Polymorphism: Ability to present the same interface for different data types.

6. Dynamic Binding: Resolving method calls at runtime.

7. Message Passing: Communication between objects.

The general rules for naming variables are:

• Names can contain letters, digits and underscores

• Names must begin with a letter or an underscore (_)

• Names are case-sensitive (myVar and myvar are different variables)

• Names cannot contain whitespaces or special characters like !, #, %, etc.

• Reserved words (like C++ keywords, such as int) cannot be used as names

How to do printf in C++;

#include <iostream>
using namespace std;

int main() {
int x = 5, y = 6, z = 50;
cout << x + y + z;
return 0;
}
How to do Scanf in C++;

#include <iostream>

using namespace std;

int main () {

x;

cout << "Type a number: "; // Type a number and press enter

cin >> x; // Get user input from the keyboard

cout << "Your number is: " << x;

return 0;

C++ Comments

Comments can be used to explain C++ code, and to make it more readable. It can also be used
to prevent execution when testing alternative code. Comments can be singled-lined or multi-
lined.

Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

C++ Multi-line Comments

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:


C++ Variables

Variables are containers for storing data values.

In C++, there are different types of variables (defined with different keywords), for example:

• int - stores integers (whole numbers), without decimals, such as 123 or -123

• double - stores floating point numbers, with decimals, such as 19.99 or -19.99

• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes

• string - stores text, such as "Hello World". String values are surrounded by double quotes

• bool - stores values with two states: true or false

Declare Multiple Variables

To declare more than one variable of the same type, use a comma-separated list:

#include <iostream>

using namespace std;

int main() {

int x, y, z;

x = y = z = 50;

cout << x + y + z;

return 0;

}
C++ Identifiers

All C++ variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and


maintainable code:

int minutesPerHour = 60;

Constants

When you do not want others (or yourself) to change existing variable values, use
the const keyword (this will declare the variable as "constant", which means unchangeable and
read-only):

Example

const int myNum = 15; // myNum will always be 15

C++ Data Types

a variable in C++ must be a specified data type:

Example

int myNum = 5; // Integer (whole number)


float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
Basic Data Types
Data Type Size Description

boolean 1 byte Stores true or false values

char 1 byte Stores a single character/letter/number, or ASCII values

int 2 or 4 bytes Stores whole numbers, without decimals

float 4 bytes Stores fractional numbers, containing one or more decimals.


Sufficient for storing 6-7 decimal digits

double 8 bytes Stores fractional numbers, containing one or more decimals.


Sufficient for storing 15 decimal digits

The data type specifies the size and type of information the variable will store:

You might also like