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

A. C++ Provides Built Objects For Input/output Operations Inside Iostream.h

1. The document discusses differences between C and C++ and key object-oriented programming concepts in C++. 2. It provides examples of using structures in C++ with encapsulation and default arguments. 3. Object-oriented programming pillars like encapsulation, abstraction, polymorphism and inheritance are explained.

Uploaded by

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

A. C++ Provides Built Objects For Input/output Operations Inside Iostream.h

1. The document discusses differences between C and C++ and key object-oriented programming concepts in C++. 2. It provides examples of using structures in C++ with encapsulation and default arguments. 3. Object-oriented programming pillars like encapsulation, abstraction, polymorphism and inheritance are explained.

Uploaded by

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

Migration from C to C++

1. No need of format specifiers


a. C++ provides built objects for input/output operations inside iostream.h
i. cin for input
ii. cout for general output
iii. cerr for error output
b. Use special operator << and >> with these objects
c. Examples
i. int n;
ii. cout << "Enter a number : "; //Insertion operator
iii. cin>> n; // extraction operator
2. C++ provides a feature of encapsulation which makes possible to place the data
members and functions within the structure.
3. In C++, structures are treated as pure data type and no typedef is required to re-
define the structures as type
4. File extension changed from .c to .cpp
5. Include every header file
6. Introduction of new comments //
7. C++ allows to create multiple functions with the same name with different
number of arguments or type of arguments.
8. C++ allows to define the default parameters in functions

Example 1
#include<iostream.h>
#include<conio.h>
//Using Default Arguments
void area(int l=5, int w=6)
{
cout << l*w << endl;
}
void main()
{
area(); //30
area(8); //48
area(9,8); //72
getch();
}

Example 2
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
//Create a structure for as student, read data and show it
struct student
{
int rollno;
char name[50];
char course[50];
void getdata()
{
cout << "Enter Roll no : "; cin >> rollno;
cout << "Name : ";fflush(stdin);gets(name);
cout << "Course : ";fflush(stdin);gets(course);
}
void showdata()
{
cout << "Roll No : " << rollno << endl;
cout << "Name : " << name << endl;
cout << "Course : " << course << endl;
cout << "Press any key to continue...";
getch();
}
};

void main()
{
student s;
s.getdata();
s.showdata();
}

Example 3
#include<iostream.h>
#include<conio.h>
//Using multiple functions with same name
void area(int side)
{
cout << "Area of square is : " << side*side << endl;
}
void area(double r)
{
cout << "Area of circle is : " << 3.14*r*r << endl;
}
void area(int len, int width)
{
cout << "Area of rectangle is : " << len*width << endl;
}

void main()
{
area(8);
area(5.7);
area(4,9);
getch();
}

9. All members of a structure are directly accessible by the structure variables.


Anybody can put any value into those variables. There is no controlling on
structure variables.

#include<conio.h>
#include<iostream.h>
struct employee
{
int empid;
char name[50];
int basic;
};
void main()
{
employee e;
e.basic=-9000;
e.empid=-77;
cout << e.basic;
getch();
}

10. Structures do not provide any re-usability of data members or functions

To remove all such problems, Bjarne Stroustup introduced Object Oriented programming
concepts in C and called as C with OOPs and later as C++

What is OOPs?

OOPs stands for Object Oriented Programming System, which is a system made of
certain components called pillars of OOPs to achieve a goal “Better Project
Management” using a programming language.

It is independent of any language.

Components or Pillars of OOPs are

1. Encapsulation
2. Abstraction
3. Polymorphism
4. Inheritance

OOPs get implemented three more elements class, object and reference.
Encapsulation means defining all members related with an entity at once place. None of
the members should be accessible outside class without permission.

class <classname>
{
//members
};

Abstraction is used to define the accessibility level of class members. C++ provided
three abstraction layers
1. Private (default)
2. Public
3. Protected

Private members can be accessed within the class only.


Protected members can be accessed within the current class or in child class only.
Public members can be accessed by the object or in child class.

We need to define different sections inside a class to place the members in those sections.
Default section is private.

#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class employee
{
int empid;
char name[50];
int basic;
public:
void setName()
{
cout << "Enter your name : ";
fflush(stdin);
gets(name);
}
void getName()
{
cout << "Name is " << name;
}
};
void main()
{
employee e;
e.setName();
e.getName();
getch();
}
28.10.2010
What is an object?
An instance of a class that holds the data members and methods defined inside a class. To
initialize the fields of an instance use special method called as constructor.

C++ provides two ways for instance created

1. Static or Early Binding


2. Dynamic or Late Binding

In case of static binding, we know the size of allocation at the time of compilation.

Classname referernce(arguments);

In case of late binding, memory is allocated when we execute a program. Use new
keyword to allocate the memory and call the constructor to initialize that memory.

classname *reference=new constructor(arguments);

Constructor

Special method inside a class having some special features


1. Same name as class name
2. No return type
3. Used to initialized the fields of an instance
4. If we do not create any constructor then default constructor is created
automatically. If we create any parameterized constructor then default constructor
is not created automatically we have to create it.
5. Can be overloaded
6. Can be private as well

Sample case 1
Create a class Number having a field as num. Create method setNumber() to initialize
the num. Write another method getNumber() to get the number. Create a method cube() to
return cube of the num.

#include<conio.h>
#include<iostream.h>
#include<math.h>
class Number
{
private:
double num;
public:
void setNumber(int n)
{
num=n;
}
double getNumber()
{
return num;
}
double cube()
{
return pow(num,3);
}
};

void main()
{
Number x,y;//x and y are the instances of Number class using Static binding
x.setNumber(6);
y.setNumber(7);
cout << "Cube of " << x.getNumber() << " is " << x.cube() << endl;
cout << "Cube of " << y.getNumber() << " is " << y.cube() << endl;
getch();

Sample case 2
Create a class Number having a field as num. Create method setNumber() to initialize
the num. Write another method getNumber() to get the number. Create a method cube() to
return cube of the num.

Also create a constructor to initialize the field num.

//Using constructor
#include<conio.h>
#include<iostream.h>
#include<math.h>
class Number
{
private:
double num;
public:
Number() //Constructor overloading
{
}
Number(int n)
{
num=n;
}
void setNumber(int n)
{
num=n;
}
double getNumber()
{
return num;
}
double cube()
{
return pow(num,3);
}
};

void main()
{
Number x(5),y(7);//Static binding with parameterized constructor
cout << "Cube of " << x.getNumber() << " is " << x.cube() << endl;
cout << "Cube of " << y.getNumber() << " is " << y.cube() << endl;
getch();

Sample case 3
Create a class Number having a field as num. Create method setNumber() to initialize
the num. Write another method getNumber() to get the number. Create a method cube() to
return cube of the num.

Also create a constructor to initialize the field num. Create two instances using late
binding and initialized with them 3.4 and 6.7

Note

1. To de-allocate the memory allocated with new, use delete operator


2. If the data field name and parameter name are same then use this pointer to refer
the current object
//Using constructor
#include<conio.h>
#include<iostream.h>
#include<math.h>
class Number
{
private:
double num;
public:
Number() //Constructor overloading
{
}
Number(double num)
{
this->num=num;
}
void setNumber(double num)
{
this->num=num;
}
double getNumber()
{
return num;
}
double cube()
{
return pow(num,3);
}
};

void main()
{
Number *x=new Number(3.4);
Number *y=new Number(6.7);
cout << "Cube of " << x->getNumber() << " is " << x->cube() << endl;
cout << "Cube of " << y->getNumber() << " is " << y->cube() << endl;
delete x;
delete y;
getch();

Example

Create a class Customer having information account no, name, balance. Create a
constructor to initialize the fields. Create method deposit(), withdraw() and
showBalance()

Now create two instances as a and b for class customer. Create a using static binding and
b as late binding.

#include<conio.h>
#include<iostream.h>
class Customer
{
int acno;
char name[51];
int balance;
public:
Customer(int acno, char *name, int opamount)
{
this-> acno=acno;
strcpy(this->name,name);
balance=opamount;
}
void deposit(int amount)
{
balance+=amount;
}
void withdraw(int amount)
{
balance-=amount;
}
void showBalance()
{
cout << endl << "Balance of " << name << " is " << balance;
}
};

void main()
{
Customer a(345,"Rakesh Kumar",6000); //static binding
a.deposit(4000);
a.showBalance();

Customer *b=new Customer(987,"Vikas Verma",9000); //late binding


b-> withdraw(6000);
b->showBalance();
getch();
}

09.11.2010

Types of members inside a class

A class can have members of two types


 Static members or class members
 Non-Static or Instance Members

Class members or static members do not need an object to call them. They get called
directly using the class name and scope resolution operator (::). Use static keyword to
define such members.

Non-static or Instance members always need an object to call them.


Example
Create a class Number with a method factorial() having an argument as num.

#include<iostream.h>
class Number
{
public:
static long factorial(int num)
{
if(num==1)
return 1;
else
return num*factorial(num-1);
}

};
void main()
{
cout << "Enter a Number : " ;
int num;
cin>> num;
cout << "Factorial of " << num << " is " << Number::factorial(num);
}

We can define the methods inside the class or outside the class. To define the methods
outside the class, declare them inside the class.

Use Scope resolution operator (::) to create the method outside the class with class name
and method name.

#include<iostream.h>
class Number
{
public:
static long factorial(int);

};

long Number::factorial(int num)


{
if(num==1)
return 1;
else
return num*factorial(num-1);
}
void main()
{
cout << "Enter a Number : " ;
int num;
cin>> num;
cout << "Factorial of " << num << " is " << Number::factorial(num);
}

Polymorphism

A concept which allows to use an item for multiple task. It get implemented using
overloading.

C++ allows two kind of overloading.

 Method Overloading or Function Overloading


 Operator Overloading

Method Overloading

When a class contains two or more methods with same name but different number of
arguments or type of arguments, it is called as method overloading.

Example
Create a class General with two methods to calculate cuberoot of a number, first one as
non-static and other one as static.

//Example of Method Overloading


#include<math.h>
#include<iostream.h>
#include<conio.h>
class General
{
private:
double num;
public:
General()
{
num=0;
}
General(double num)
{
this->num=num;
}
double cuberoot()
{
cout << "Cube root of " << num << " is " << pow(num,1.0/3)
<<endl;
}
static double cuberoot(double n)
{
cout << "Cube root of " << n << " is " << pow(n,1.0/3) << endl;
}
};

void main()
{
General x(6.0);
clrscr();
General::cuberoot(9.0);
}

Operator Overloading

When an operator can be use with an object to perform some actions other defined pre-
defined actions, it is called as operator overloading.

Test Case

What is the output of


int num=6;
cout << num << 2 << (num << 2);

Operations
cout << num << 2 << 24;

Output
6224

To customize working of an operator use operator keyword with some operator and
function that will handle the working of the operation.

It is also a kind of function when function does not have its name.

#include<iostream.h>
#include<string.h>
#include<conio.h>
class string
{
private:
char str[50];
public:
string(char *s)
{
strcpy(str,s);
}
char * operator + (string x)
{
char s[100];
strcpy(s,str);
strcat(s,x.str);
return s;
}
char * operator + (char *other)
{
char s[100];
strcpy(s,str);
strcat(s,other);
return s;
}

};
void main()
{
string x="Vikas";
string y="Verma";

char *fullname=x + "Delhi";


clrscr();
cout << fullname;
}

Assignment

Create a class Size having two fields feet and inch. Create constructors to initialize the
fields. Create a method addsize() that takes two sizes as argument and returns the new
size. Create a method show() to show the current size.

Overloading + operator to add two sizes.

Defines all methods outside the class.


11.11.2010

Inheritance

Most important pillar of OOPs that provides re-usability of code, reducing the code size
and increase the performance of the project.

A class can inherit members of other classes using : operator

class A
{
};

class B : A
{
};

Only protected and public member can be inherited in child class.

Inheritance of three types.


1. Privately (default)
2. Protectly
3. Publicly

1.
class B : A
{
};

Or
class B : private A
{
};

2.

class B : protected A
{
};

3.
class B : public A
{
};
Depending on type of inheritance, members of parent class goes to different sections of
child class.

Privately means all protected and public members of parent class goes to private area of
child class.

Protectly means all protected and public members of parent class goes to protected area
of child class.

Publicly means all protected members of parent class goes to protected section of child
class and public members goes to public section of child class.

Inheritance in C++ can be categorized as

1. Single Inheritance
2. Multiple Inheritance
3. Multi Level Inheritance
4. Hybrid Inheritance

Single
class A
{
};
class B : A
{
};

Multiple

class C : A,B
{
};

Multi level

class D : B
{
};

Hybrid

class X : C, D
{
};
//Using Inheritance
#include<iostream.h>
#include<conio.h>
class Num2
{
private:
int a,b;
public:
Num2(int a, int b)
{
this->a=a;
this->b=b;
}
int p2()
{
return a*b;
}
};
class Num3 : public Num2
{
private:
int c;
public:
Num3(int a, int b, int c) : Num2(a,b)
{
this->c=c;
}
int p3()
{
return p2()*c;
}
};

void main()
{
Num3 x(5,6,7);
clrscr();
cout << x.p3() << endl;
cout <<x.p2();
}

Method Hiding

A class can hiding method of parent class. In such case, members of parent class will not
be visible though child class.
A method in child class, having the same signature and different body contents as in
parent class is called as method hiding.

Or

A method in parent class, re-written in child class with same signature and different body
contents is called as method hiding.

//Using Inheritance
#include<iostream.h>
#include<conio.h>
class Num2
{
private:
int a,b;
public:
Num2(int a, int b)
{
this->a=a;
this->b=b;
}
int product()
{
return a*b;
}
};
class Num3 : public Num2
{
private:
int c;
public:
Num3(int a, int b, int c) : Num2(a,b)
{
this->c=c;
}
int product() //Method Hiding
{
return Num2::product()*c;
}
};

void main()
{
Num3 x(5,6,7);
clrscr();
cout << x.product();
}
Note:
In inheritance, a parent can hold reference to its childs and can access only those methods
of child, whose signature is provided from parent to the child.

Method Overriding

A virtual or pure virtual method of a class, re-written in child class with same signature
and different body contents is called as method overriding.

To define a virtual or pure virtual method, use virtual keyword.

It get used to implement the Runtime Polymorphism. When same reference can be used
to access members of child classes, it is called as runtime polymorphism.

//Using Method Overriding


#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()
{
cout << endl << "Calling from A";
}
};

class B : public A
{
public:
void show() //Method Overriding
{
cout << endl << "Calling from B";
}
void hi()
{
cout << endl << "Hi to all from B";
}
};
class C : public B
{
public:
void show() //Method Overriding
{
cout << endl << "Calling from C";
}
};
void main()
{
A *p; //Runtime Polymorphisms
clrscr();
p=new A();
p->show();
p=new B();
p->show();
p=new C();
p->show();
}

16.11.2010

What is pure virtual?

A method inside a class having the signature but no body contents assigned the value as 0
is called as pure virtual methods.

class X
{
public:
virtual void show()=0;
};

Here is show() is a pure virtual method.

Such methods are always overridden by child class.


A class that contains such kind of methods is called as abstract class.

We can never create an object of such kind of classes but reference can be created.

Such classes are used for division of work or creating modules in the project. All such
classes get implemented in a class. We create the reference of abstract class and object of
child class. It allows to call only those methods of child whose signature is provided that
abstract class.

Example

#include<iostream.h>
#include<conio.h>
class Hr //Abstract class
{
public:
virtual void salaryInfo()=0; //pure virtual method
};

class Finance
{
public:
virtual void budget()=0;
};

class ERP : public Hr, public Finance


{
public:
void salaryInfo() //overriding
{
cout <<endl<< "Salary will be given on 7th of Every month";
}
void budget()
{
cout <<endl << "Budget for 2011 is 10 Cr";
}
};

void main()
{
Hr *h=new ERP();
h->salaryInfo();
getch();
}

What is class diamond problem?


or
What is the problem faced in multiple inheritance in C++? How to resolve it?

When we inherit a class into two other classes and those two classes are further inherited
in other classes then the method of parent class goes to child from two paths. Such
problem is called as classic diamond problem. It create an ambiguity in application.

To avoid the ambiguity in such applications, C++ introduced a concept of virtual base
class. When we declare a class as virtual base class, its method get placed in a table
called VPTR table or Virtual Pointer Table. In case of ambiguity methods are taken from
that table.

#include<iostream.h>
#include<conio.h>
class A
{
public :
void show()
{
cout << endl << "Calling from A";
}
};

class B: virtual public A //virtual base class


{
};

class C: virtual public A


{
};

class D: public B, public C


{
};

void main()
{
D *p=new D();
p->show();
getch();
}

What are different kinds of classes in C++?

1. Concrete class
a. Allows to create an object
2. Abstract class
a. Do not allow to create an object

What are the friends?


Can we access private data of an object? If yes, how?

C++ allows an mechanism to access private data of an object using friends. Friends can
be of two types.
1. Friend functions
2. Friend class

Friend functions is a general functions in a program that can be allowed to access


private data of an object if declared as friend.

Use friend keyword to declare a function as friend.

#include<iostream.h>
#include<math.h>
#include<conio.h>
class General
{
private:
int num;
public:
General(int g)
{
num=g;
}
void square()
{
cout << "Square of " << num << " is " << num*num << endl;
}
friend void squareroot(General); //friend function
};

void squareroot(General p)
{
cout << "Square root of " << p.num << " is " << pow(p.num,1.0/2);
}
void main()
{
General x(5);
x.square();
squareroot(x);
getch();
}

A friends class allows to access private data of another class in any of its methods.

#include<iostream.h>
#include<math.h>
#include<conio.h>
class General
{
private:
int num;
public:
General(int g)
{
num=g;
}
void square()
{
cout << "Square of " << num << " is " << num*num << endl;
}
friend class Test;//friend class
};
class Test
{
public:
void squareroot(General p)
{
cout << "Square root of " << p.num << " is " <<
pow(p.num,1.0/2);
}
};
void main()
{
General x(5);
x.square();
Test t;
t.squareroot(x);
getch();
}

Running an application in C

Use system() function

You might also like