A. C++ Provides Built Objects For Input/output Operations Inside Iostream.h
A. C++ Provides Built Objects For Input/output Operations Inside Iostream.h
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();
}
#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();
}
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.
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
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.
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.
Constructor
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.
//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
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();
09.11.2010
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.
#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);
};
Polymorphism
A concept which allows to use an item for multiple task. It get implemented using
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.
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
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";
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.
Inheritance
Most important pillar of OOPs that provides re-usability of code, reducing the code size
and increase the performance of the project.
class A
{
};
class B : A
{
};
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.
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.
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.
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
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;
};
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;
};
void main()
{
Hr *h=new ERP();
h->salaryInfo();
getch();
}
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";
}
};
void main()
{
D *p=new D();
p->show();
getch();
}
1. Concrete class
a. Allows to create an object
2. Abstract class
a. Do not allow to create an object
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
#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