Professional Documents
Culture Documents
MODULE 1 1
20CS411 – DATA STRUCTURES USING C++
MODULE – 1
C++ PROGRAMMING
An overview of C++ - Difference between procedure and object oriented
programming language, Data Types, Variables, Operators, Expressions
and Statements-Functions and Arrays- C++ Class Overview- Class
Definition, Objects, Class Members, Access Control, Constructors and
destructors, parameter passing methods, Inline functions, static class
members, this pointer, friend functions, dynamic memory allocation and
deallocation (new and delete) - Inheritance basics, base and derived
classes, inheritance types, runtime polymorphism using virtual functions,
abstract classes - Generic Programming- Function and class templates.
MODULE 1 2
20CS411 – DATA STRUCTURES USING C++
1.4 Functions
1.5 Arrays
1.6 C++ Class Overview- Class Definition, Objects, Class Members
MODULE 1 3
20CS411 – DATA STRUCTURES USING C++
and delete)
1.11 Inheritance basics, base and derived classes, inheritance types
MODULE 1 4
20CS411 – DATA STRUCTURES USING C++
MODULE 1 5
20CS411 – DATA STRUCTURES USING C++
Public
• Members are accessible from outside the class using the
direct member access operator (.) with the object of that
class.
Private
• Only the member functions or the friend functions are
allowed to access the private data members of a class.
• 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.
MODULE 1 6
20CS411 – DATA STRUCTURES USING C++
MODULE 1 7
20CS411 – DATA STRUCTURES USING C++
int main() {
MyClass myObj;
myObj.x = 25;
myObj.y = 50;
return 0;
}
MODULE 1 8
20CS411 – DATA STRUCTURES USING C++
MODULE 1 9
20CS411 – DATA STRUCTURES USING C++
MODULE 1 10
20CS411 – DATA STRUCTURES USING C++
MODULE 1 11
20CS411 – DATA STRUCTURES USING C++
MODULE 1 15
20CS411 – DATA STRUCTURES USING C++
MODULE 1 17
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
Constructor :
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is
created.
• It must be placed in public section of class.
• If we do not specify a constructor, C++ compiler generates
a default constructor for object (expects no parameters and
has an empty body).
• Declare a constructor as protected or private.
MODULE 1 18
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
MODULE 1 19
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
Default Constructor :
• No parameter
• Invoked at the time of creating object.
• If no constructors are declared in a class, the compiler
provides an implicit inline default constructor.
MODULE 1 20
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
#include <iostream> Constructor Program
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{ Output:
Employee e1; Default Constructor Invoked
Employee e2; Default Constructor Invoked
return 0;
}
MODULE 1 21
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
Parameterized Constructor
• Constructors with parameter
• Different values to data members of different objects
• Passing the appropriate values as argument.
• Take parameters (just like regular functions)
class Cube
{
public:
int side;
Cube(int x) // Parameterized Constructor
{
side=x;
}
};
MODULE 1 22
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
Parameterized Constructor Program
#include <iostream> void display()
using namespace std; {
class Employee { cout<<id<<" "<<name<<"
public: "<<salary<<endl;
int id; }
string name; };
float salary; int main(void) {
Employee(int i, string n, float s) Employee e1 =Employee(101,
{ "Sonoo", 890000);
id = i; Employee e2=Employee(102, "Nakul",
name = n; 59000);
salary = s; e1.display();
} e2.display();
return 0;
}
MODULE 1 23
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
Copy Constructor :
• Copy values of data members of one object into other
object
• Constructors which takes an object as argument
MODULE 1 24
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
1.7 Constructor
Constructor Overloading
• Overloaded constructors must have the same name and
different number of arguments
• Overloading achieved based on the number of parameter and
types of the arguments are passed.
• Argument is passed while creating objects
class Rect{
private:
int area;
public:
Rect(){ area = 0; }
Rect(int a, int b){ area = a * b; }
MODULE 1 26
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
Constructor Overloading Program
class Student
{ int main()
public: {
int rollno; Student A(10);
string name; Student B(11, "John");
// first constructor }
Student(int x)
{
rollno = x;
name = "None";
}
// second constructor
Student(int x, string str)
{
rollno = x;
name = str;
}
};
MODULE 1 27
20CS411 – DATA STRUCTURES USING C++
1.7 Destructor
~class_name()
Syntax {
//Some code
}
MODULE 1 28
20CS411 – DATA STRUCTURES USING C++
1.7 Destructor
Destructor Program
class A
{ int main()
// constructor {
A() A obj1; // Constructor Called
{ int x = 1
cout << "Constructor called"; if(x)
} {
A obj2; // Constructor Called
// destructor } // Destructor Called for obj2
~A() } // Destructor Called for obj1
{
cout << "Destructor called";
}
};
MODULE 1 29
20CS411 – DATA STRUCTURES USING C++
1.7 Constructor
MODULE 1 30
20CS411 – DATA STRUCTURES USING C++
Try Yourself
1. Imagine a tollbooth with a class called toll Booth. The two data
items are a type unsigned int to hold the total number of cars,
and a type double to hold the total amount of money collected. A
constructor initializes both these to 0. A member function called
payingCar ( ) increments the car total and adds 0.50 to the cash
total. Another function, called nopayCar ( ), increments the car
total but adds nothing to the cash total. Finally, a member
function called displays the two totals.
2. Write a C++ program to generate Fibonacci Series. Use
Constructor to Initialize the Data Members.
MODULE 1 31
20CS411 – DATA STRUCTURES USING C++
Try Yourself
MODULE 1 32