BCS306B Module 1 PDF
BCS306B Module 1 PDF
Module – 1
Chapter 1 :
An Overview of C++
By,
SYLLABUS:
[email protected]
Mobile: 9611699567
What is C++
C++ is a high-level, object oriented, general-purpose programming language
1. Operating Systems
Mac OS X has large amounts of code written in C++. Most of the software from Microsoft like Windows, Microsoft Office, IDE Visual Studio, and Internet
Explorer are also written in C++.
2. Games
It is used in 3D games and multiplayer networking.
4. Web Browsers
Mozilla Firefox is completely developed from C++. Google applications like
5. Embedded Systems
Various embedded systems that require the program to be closer to hardware
6. Banking Applications
Infosys Finacle is a popular banking application developed using C++.
7. Compilers
The compilers of many programming languages are developed in C and C++.
3. The input and output is done using The input and output is done using
scanf and printf statements. cin and cout statements.
4. The I/O operations are supported by The I/O operations are supported
stdio.h header file. by iostream.h header file.
5. C does not support inheritance, polymorphism, class and C++ supports inheritance, polymorphism, class and object concepts.
object concepts.
6. The data type specifier or format specifier ( %d, %f, %c) is required in printf The format specifier is not required
and scanf functions. in cin and cout functions.
int main()
{
int A, B, sum = 0;
// Ask user to enter the two numbers
printf("Enter two numbers A and B : \n");
// Read two numbers from the user || A = 2, B = 3
scanf("%d%d", &A, &B);
// Calculate the addition of A and B
// using '+' operator
sum = A + B;
// Print the sum
printf("Sum of A and B is: %d", sum);
return 0;
}
{
int A, B, sum;
C++ manipulator endl function is used to insert a new
cout << "Please enter the First Number : "<<endl;
cin >> A; line character and flush the stream.
cout << "Please enter the Second Number : "<<endl;
cin >> B; sum = A + B;
cout << "Sum of Two Numbers " << A <<" and " << B << " = " << sum;
return 0;
}
iostream stands for standard input-output stream. This header file contains
definitions of objects like cin, cout, cerr, etc.
The std is a short form of standard, the std namespace contains the built-in classes
and declared functions.
Single-line comments start with two forward slashes (//)
Multi-line comments start with /* and ends with */.
cin uses the insertion operator( >> ) while cout uses the extraction operator( << ).
// C++ Program to show the syntax/working of Objects as a
// part of Object Oriented programming
#include <iostream>
using namespace std;
public:
void getdetails() {}
};
int main()
{
person p1; // p1 is a object return 0;
}
public - members are accessible from outside the class.
private - members cannot be accessed (or viewed) from outside the class.
protected - members cannot be accessed from outside the class,
however, they can be accessed in inherited classes.
Q : Differentiate between Procedure Oriented Programming and
Object Oriented Programming.
Functions are preferred over data. Security and accessibility are preferred.
the size of the problem is small, POP the size of the problem is big, OOP is
preferred. preferred.
C++, C#, Java, Python, etc. are the BASIC, COBOL, Pascal, etc. are the
examples of OOP languages. examples POP languages.
The object-oriented programming is basically a computer programming design methodology that models software design around data or objects
The main aim of OOP is to bind together the data and the functions that
operate on them.
here are some basic concepts that act as the building blocks of OOPs i.e.
Class
Inheritance
Objects
Dynamic Binding
Encapsulation
Message Passing
Abstraction
Polymorphism
Q: Explain the salient features of object oriented programming
languages.
1. CLASS
2. OBJECT
3. ENCAPSULATION
single unit.
Abstraction means displaying only essential information and hiding the details.
Data abstraction refers to providing only essential information about the data
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than
one form. A person at the same time can have different characteristics.
Function Overloading: Function overloading is using a single function name to perform different types of tasks. Polymorphism is extensively used in
implementing inheritance.
6. INHERITANCE
Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
Super Class: The class whose properties are inherited by a sub-class is called Base Class or Superclass.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of
the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.
7. DYNAMIC BINDING
is decided at runtime.
C++ has virtual functions to support this. Because dynamic binding is flexible, it avoids the drawbacks of static binding, which connected the function call
and definition at build time.
MESSAGE PASSING
information.
A message for an object is a request for the execution of a procedure and therefore will invoke a function in the receiving object that generates the
desired results.
member functions, which can be accessed and used by creating an instance of that class.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is
allocated.
Defining Class
A class is defined in C++ using the keyword class followed by the name of the class. The body of the class is defined inside the curly brackets and terminated
by a semicolon at the end.
Declaring Objects
When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class,
you need to create objects.
Syntax
ClassName ObjectName;
Accessing member functions: The data members and member functions of the class can be accessed using the dot(‘.’) operator with the object. For
example, if the name of the object is obj and you want to access the member function with the name printName() then you will have
to write obj.printName().
The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the
object. Accessing a data member depends solely on the access control of that data member.
class AIT {
// Access specifier
public:
// Data Members
string CSE;
// Member Functions()
void printname() { cout << “CSE name is:" << CSE; }
};
int main()
{
// Declare an object of class geeks
AIT obj1;
// accessing data member
RD
obj1.CSE = “3 A & B";
// accessing member function
obj1.printname();
return 0;
}
1.3 GENERAL FORM OF C++ PROGRAM
Although individual styles will differ, most C++ programs will have this general
form:
#includes
base-class declarations derived class declarations
nonmember function prototypes int main( )
{
//...
}
nonmember function definitions
In most large projects, all class declarations will be put into a header file and included with each module. But the general organization of a
program remains the same.
Module – 1
Chapter 2 :
Classes and Objects
1.4 CLASSES :
Classes are created using the keyword class. A class declaration defines a new type that links code and data. This new type is then used to declare objects
of that class.
class class-name {
private data and functions access-specifier:
data and functions access-specifier:
Create an Object :
To create an object of MyClass, specify the class name, followed by the object
name.
To access the class attributes (myNum and myString), use the dot syntax (.)
on the object:
Example :
Create an object called "myObj" and access the attributes:
int main() {
MyClass myObj; // Create an object of MyClass
By default, functions and data declared within a class are private to that
Once an access specifier has been used, it remains in effect until either another access specifier is encountered or the end of the class declaration is
reached.
A class can have multiple public, protected, or private labeled sections. Each section remains in effect until either another section label or the closing
class Base {
};
1.5 FRIEND FUNCTION
Similarly, protected members can only be accessed by derived classes and are inaccessible from outside. For example,
class MyClass {
private: int member1;
}
int main()
{
MyClass obj;//Error! Cannot access private members
obj.member1 = 5;
}
However, there is a feature in C++ called friend functions that break this rule and allow us to access member functions from outside the class.
A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the
class.
class className {
... .. ...
... .. ...
}
#include <iostream>
using namespace std;
class demo
{
private:
int a=5;
friend void display(demo);//friend function declaration
};
int main()
{
demo d;
display(d); //calling a friend function
return 0;
}
1.6 FRIEND CLASSES
We can also use a friend Class in C++ using the friend keyword. For example,
class ClassB;
class ClassA {
// ClassB is a friend class of ClassA friend class ClassB; ... .. ...
}
class test2
class ClassB { {
... .. ... test1 t1; public:
}
void put()
When a class is declared a friend class, all the {
t1.get();
member functions of the friend class become friend
cout<<"a: "<<t1.a<<endl;
functions. cout<<"b: "<<t1.b<<endl;
}
Since ClassB is a friend class, we can access all };
{
int a,b;
public:
void get()
{
cout<<"enter 2 integers"; cin>>a>>b;
}
friend class test2;
//defining friend class
};
}
1.7 INLINE FUNCTIONS
In C++, we can declare a function as inline. This copies the function to the location of the function call in compile-time and may make the program
execution faster.
displayNum(5);
displayNum(8);
Output: 5 8 666
Here is how this program works:
Working of inline functions in C++
Here, we created an inline function named displayNum() that takes a single integer as a parameter.
We then called the function 3 times in the main() function with different arguments. Each time displayNum() is called, the compiler copies the code of the function
to that call location.
1.8 Parameterized Constructors :
A constructor is a special type of member function that is called automatically
In C++, a constructor has the same name as that of the class and it does not have a return type. For example,
class Wall {
public:
{ // code }
};
#include <iostream>
class A {
private:
num1 = n1;
num2 = n2; }
void display() {
}};
int main()
Output :
num1 = 3
num2 = 8
1.9 STATIC CLASS MEMBERS
Static data members are class members that are declared using
static keywords.
Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
It is initialized before any object of this class is created, even before the main starts.
It is visible only within the class, but its lifetime is the entire program.
Syntax:
A static member function can only access static data members of the
OUTPUT :
class, This is a static member function
1.10 WHEN
CONSTRUCTORS
and it cannot access non-static data members or member functions AND
DESTRUCTORS ARE
EXECUTED
of the class.
list);
class ClassName {
A constructor is a particular member function having the same
// other members name as the class name. It calls automatically whenever the
static return_type function_name(parameter
object of the class is created.
list);
};
Destructors have the same class name preceded by (~) tilde
Where “ClassName” is the name of the class, “return_type” is the symbol. It removes and destroys the memory of the object, which
return type of the function, “function_name” is the name of the the constructor allocated during the creation of an object.
function, and “parameter list” is the list of parameters passed to the A constructor is a function that initializes the object of the class
function.
and allocates the memory location for an object,
#include <iostream>
using namespace std; Destructor also has the same name as the class name, denoted by
class class_name
{
……….
public
class_name([parameter list])
{
……………….
}
};
is optional.
Syntax:
The syntax of destructor in C++ are
given below.
class class_name
{
…………….;
…………….;
public:
xyz(); //constructor
~xyz(); //destructor
};
Here, we use the tilde symbol for defining the destructor in C++ programming.
The Destructor has no argument and does not return any value, so it cannot
be overloaded.
There some features which distinguish between a constructor and a normal member function
If we do not specify a constructor while defining a class, the C++ compiler generates a default constructor for us without any parameters and an
empty body.
#include <iostream.h>
#include<conio.h>
class MyClass
{
public:
int x;
MyClass(); // constructor
~MyClass(); // destructor
};
// Implement MyClassconstructor.
MyClass::MyClass()
{
x = 10;
}
// Implement MyClass destructor.
MyClass::~MyClass()
{
); MyClass ob1; MyClass ob2;
}
cout <<ob1.x<< " " <<ob2.x<<"\n";
void main() getch();
{
Output:
10 10
cout<< "Destructing
...\n";
c
l
r
s
c
r
(
S.N Constructors Destructors
o.
1. The constructor initializes the class and allots the memory to an If the object is no longer required, then destructors demolish the objects.
object.
2. When the object is created, a constructor is called automatically. When the program gets terminated, the destructor is called
automatically.
4. A constructor allows an object to initialize some of its value A destructor allows an object to execute some code at the time of its
before it is used. destruction.
6. When it comes to constructors, there can be various constructors in a When it comes to destructors, there is constantly a single destructor in
class. the class.
7. They are often called in successive They are often called in reverse
order. order of constructor.
1.11 THE SCOPE RESOLUTION OPERATOR
when there is a local variable or member function with the same name.
For example, when the global and local variable or function has the same name in a program, and when we call the variable, by default it only accesses
the inner or local variable without calling the global variable. In this way, it hides the global variable or function.
To overcome this situation, we need to use the scope resolution operator to fetch a program’s hidden variable or function.
#include <iostream>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle (int l, int b)
{
length = l;
breadth = b;
}
int area ()
{
return length * breadth;
}
int perimeter ();
};
int Rectangle::perimeter()
{
return 2*(length + breadth);
}
int main()
{
Rectangle r(8, 3);
cout << "Area is :"<< r.area() <<endl; cout << "Perimeter is :"<<
r.perimeter();
Output: Area is : 24
Perimeter is :22
To make you understand these two methods, please have a look at the below Rectangle class. Here, the Rectangle class has two private data members i.e.
The Rectangle class also has one parameterized constructor to initialize the length and breadth data members. But here, from the below class let us keep
C++ programming, we can pass objects to a function in a similar manner as passing regular arguments.
public:
int age = 20;
char ch = 'A';
Pass by Value
int main()
{ This creates a shallow local copy of the object in the function scope. Things you modify
A obj; obj.display(obj); return 0;
here won't be reflected in the object passed to it.
}
Pass by Reference
Output
Age = 20 This passes a reference to the object to the function. Things you modify here will be reflected in
Character = A
the object passed to it. No copy of the object is created.
An object can be passed to a function just like we pass structure to a function. Here in class A we have a function display() in which we are passing the object of
class A.
Similarly we can pass the object of another class to a function of different class.
1.13 Returning Objects
A function may return an object to the caller
#include <iostream>
using namespace std;
class Student
{
public:
double marks1, marks2;
};
return student;
}
int main() {
Student student1;
// Call function
student1 = createStudent();
return 0;
}
Output
Marks 1 = 96.5
Marks 2 = 75.0
In this program, we have created a function createStudent() that returns an
object of Student class.
We have called createStudent() from the main() method.
Assuming that both objects are of the same type, you can assign one object to another. This causes the data of the object on the right side to be copied into the data
of the object on the left. For example, this program displays 99:
int i;
public:
};
int main()
ob1.seti(99);