c++ notes ut 2
c++ notes ut 2
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Object
Any entity that has state and behavior is known as an object. For
example: chair, pen, table, keyboard, bike etc. It can be physical and
logical.
Class
Collection of objects is called class. It is a logical entity.
Inheritance
When one object acquires all the properties and behaviours of
parent object i.e. known as inheritance. It provides code reusability. It is
used to achieve runtime polymorphism.
Polymorphism
When one task is performed by different ways i.e. known as
polymorphism. For example: to convince the customer differently, to draw
something e.g. shape or rectangle etc.
Abstraction
Hiding internal details and showing functionality is known as
abstraction. For example: phone call, we don't know the internal
processing.
Encapsulation
Binding (or wrapping) code and data together into a single unit is
known as encapsulation. For example: capsule, it is wrapped with
different medicines.
C++ Object
In C++, Object is a real world entity, for example, chair, car, pen, mobile,
laptop etc.
In other words, object is an entity that has state and behavior. Here, state
means data and behavior means functionality.
In this example, Student is the type and s1 is the reference variable that
refers to the instance of Student class.
C++ Class
In C++, class is a group of similar objects. It is a template from which
objects are created. It can have fields, methods, constructors etc.
Let's see an example of C++ class that has three fields only.
1. class Student
2. {
3. public:
4. int id; //field or data member
5. float salary; //field or data member
6. String name;//field or data member
7. }
1. #include <iostream>
2. using namespace std;
3. class Student {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. };
8. int main() {
9. Student s1; //creating an object of Student
10. s1.id = 201;
11. s1.name = "Sonoo Jaiswal";
12. cout<<s1.id<<endl;
13. cout<<s1.name<<endl;
14. return 0;
15. }
Output:
201
Sonoo Jaiswal
Output:
201 Sonoo
202 Nakul
1. #include <iostream>
2. using namespace std;
3. class Employee {
4. public:
5. int id;//data member (also instance variable)
6. string name;//data member(also instance variable)
7. float salary;
8. void insert(int i, string n, float s)
9. {
10. id = i;
11. name = n;
12. salary = s;
13. }
14. void display()
15. {
16. cout<<id<<" "<<name<<" "<<salary<<endl;
17. }
18. };
19. int main(void) {
20. Employee e1; //creating an object of Employee
21. Employee e2; //creating an object of Employee
22. e1.insert(201, "Sonoo",990000);
23. e2.insert(202, "Nakul", 29000);
24. e1.display();
25. e2.display();
26. return 0;
27. }
Output:
C++ Constructor
In C++, constructor is a special method which is invoked automatically at
the time of object creation. It is used to initialize the data members of new
object generally. The constructor in C++ has the same name as class or
structure.
There can be two types of constructors in C++.
o Default constructor
o Parameterized constructor
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
7. {
8. cout<<"Default Constructor Invoked"<<endl;
9. }
10. };
11. int main(void)
12. {
13. Employee e1; //creating an object of Employee
14. Employee e2;
15. return 0;
16. }
Output:
#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of
Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
Output:
1. class A
2. {
3. A(A &x) // copy constructor.
4. {
5. // copyconstructor.
6. }
7. }
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6. int x;
7. A(int a) // parameterized constructor.
8. {
9. x=a;
10. }
11. A(A &i) // copy constructor
12. {
13. x = i.x;
14. }
15. };
16. int main()
17. {
18. A a1(20); // Calling the parameterized constructor.
19. A a2(a1); // Calling the copy constructor.
20. cout<<a2.x;
21. return 0;
22. }
Output:
20
When Copy Constructor is called
Copy Constructor is called in the following scenarios:
CPP14
#include <iostream>
using namespace std;
class geeks {
const char* p;
public:
// default constructor
geeks()
{
void display()
{
cout << p << endl;
}
};
int main()
{
geeks obj;
obj.display();
}
Output:
geeks
In this we point data member of type char which is allocated memory
dynamically by new operator and when we create dynamic memory within
the constructor of class this is known as dynamic constructor.
Example 2:
CPP14
#include <iostream>
using namespace std;
class geeks {
int* p;
public:
// default constructor
geeks()
{
int main()
{
Output:
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
dynamically .
In this program we have created array of object dynamically. The first object
is ptr[0], second is ptr[1] and so on . For each object creation default
constructor is called and for each object memory is allocated to pointer type
variable by new operator.
Example 3:
CPP14
#include <iostream>
using namespace std;
class geeks {
int* p;
public:
// default constructor
geeks()
{
// parameterized constructor
geeks(int x)
{
p = new int;
*p = x;
}
void display()
{
cout << *p << endl;
}
};
int main()
{
Output:
0
7
C++ Destructor
A destructor works opposite to constructor; it destructs the objects of
classes. It can be defined only once in a class. Like constructors, it is
invoked automatically.
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
7. {
8. cout<<"Constructor Invoked"<<endl;
9. }
10. ~Employee()
11. {
12. cout<<"Destructor Invoked"<<endl;
13. }
14.};
15. int main(void)
16.{
17. Employee e1; //creating an object of Employee
18. Employee e2; //creating an object of Employee
19. return 0;
20.}
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked
1. #include <iostream>
2. using namespace std;
3. class Employee {
4. public:
5. int id; //data member (also instance variable)
6. string name; //data member(also instance variable)
7. float salary;
8. Employee(int id, string name, float salary)
9. {
10. this->id = id;
11. this->name = name;
12. this->salary = salary;
13. }
14. void display()
15. {
16. cout<<id<<" "<<name<<" "<<salary<<endl;
17. }
18. };
19. int main(void) {
20. Employee e1 =Employee(101, "Sonoo", 890000); //creating
an object of Employee
21. Employee e2=Employee(102, "Nakul", 59000); //creating an
object of Employee
22. e1.display();
23. e2.display();
24. return 0;
25. }
Output:
C++ static
In C++, static is a keyword or modifier that belongs to the type not
instance. So instance is not required to access the static members. In C+
+, static can be field, method, constructor, class, properties, operator and
event.
1. #include <iostream>
2. using namespace std;
3. class Account {
4. public:
5. int accno; //data member (also instance variable)
6. string name; //data member(also instance variable)
7. static float rateOfInterest;
8. Account(int accno, string name)
9. {
10. this->accno = accno;
11. this->name = name;
12. }
13. void display()
14. {
15. cout<<accno<< "<<name<< " "<<rateOfInterest<<
endl;
16. }
17. };
18. float Account::rateOfInterest=6.5;
19. int main(void) {
20. Account a1 =Account(201, "Sanjay"); //creating an object of
Employee
21. Account a2=Account(202, "Nakul"); //creating an object of E
mployee
22. a1.display();
23. a2.display();
24. return 0;
25. }
Output:
1. #include <iostream>
2. using namespace std;
3. class Account {
4. public:
5. int accno; //data member (also instance variable)
6. string name;
7. static int count;
8. Account(int accno, string name)
9. {
10. this->accno = accno;
11. this->name = name;
12. count++;
13. }
14. void display()
15. {
16. cout<<accno<<" "<<name<<endl;
17. }
18. };
19. int Account::count=0;
20. int main(void) {
21. Account a1 =Account(201, "Sanjay"); //creating an object of
Account
22. Account a2=Account(202, "Nakul");
23. Account a3=Account(203, "Ranjana");
24. a1.display();
25. a2.display();
26. a3.display();
27. cout<<"Total Objects are: "<<Account::count;
28. return 0;
29. }
Output:
201 Sanjay
202 Nakul
203 Ranjana
Total Objects are: 3
C++ Structs
In C++, classes and structs are blueprints that are used to create the
instance of a class. Structs are used for lightweight objects such as
Rectangle, color, Point, etc.
Unlike class, structs in C++ are value type than reference type. It is useful
if you have data that is not intended to be modified after creation of
struct.
1. struct structure_name
2. {
3. // member declarations.
4. }
1. struct Student
2. {
3. char name[20];
4. int id;
5. int age;
6. }
Student s;
For example:
1. s.id = 4;
1. #include <iostream>
2. using namespace std;
3. struct Rectangle
4. {
5. int width, height;
6.
7. };
8. int main(void) {
9. struct Rectangle rec;
10. rec.width=8;
11. rec.height=5;
12. cout<<"Area of Rectangle is: "<<(rec.width * rec.height)<<
endl;
13. return 0;
14. }
Output:
1. #include <iostream>
2. using namespace std;
3. struct Rectangle {
4. int width, height;
5. Rectangle(int w, int h)
6. {
7. width = w;
8. height = h;
9. }
10. void areaOfRectangle() {
11. cout<<"Area of Rectangle is: "<<(width*height); }
12. };
13. int main(void) {
14. struct Rectangle rec=Rectangle(4,6);
15. rec.areaOfRectangle();
16. return 0;
17. }
Output:
Structure Class
The instance of the structure is known The instance of the class is known as
as "Structure variable". "Object of the class".
C++ Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which
means many forms. It is a greek word. In object-oriented programming,
we use 3 main concepts: inheritance, encapsulation, and polymorphism.
It is less flexible as mainly all the things It is more flexible as all the things
execute at the compile time. execute at the run time.
13. public:
14. void display()
15. {
16. cout<<"Class B";
17. }
18. };
In the above case, the prototype of display() function is the same in both
the base and derived class. Therefore, the static binding cannot be
applied. It would be great if the appropriate function is selected at the run
time. This is known as run time polymorphism.
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat(){
6. cout<<"Eating...";
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void eat()
13. { cout<<"Eating bread...";
14. }
15. };
16. int main(void) {
17. Dog d = Dog();
18. d.eat();
19. return 0;
20. }
Output:
Eating bread...
1. #include <iostream>
2. using namespace std;
3. class Shape { // base class
4. public:
5. virtual void draw(){ // virtual function
6. cout<<"drawing..."<<endl;
7. }
8. };
9. class Rectangle: public Shape // inheriting Shape class.
10. {
11. public:
12. void draw()
13. {
14. cout<<"drawing rectangle..."<<endl;
15. }
16. };
17. class Circle: public Shape // inheriting Shape c
lass.
18.
19. {
20. public:
21. void draw()
22. {
23. cout<<"drawing circle..."<<endl;
24. }
25. };
26. int main(void) {
27. Shape *s; // base class pointer.
28. Shape sh; // base class object.
29. Rectangle rec;
30. Circle cir;
31. s=&sh;
32. s->draw();
33. s=&rec;
34. s->draw();
35. s=?
36. s->draw();
37. }
Output:
drawing...
drawing rectangle...
drawing circle...
1. #include <iostream>
2. using namespace std;
3. class Animal { // base class declaration.
4. public:
5. string color = "Black";
6. };
7. class Dog: public Animal // inheriting Animal class.
8. {
9. public:
10. string color = "Grey";
11. };
12. int main(void) {
13. Animal d= Dog();
14. cout<<d.color;
15. }
Output:
Black
o methods,
o constructors, and
o indexed properties
Output:
30
55
Let's see the simple example when the type of the arguments vary.
1. #include<iostream>
2. using namespace std;
3. int mul(int,int);
4. float mul(float,int);
5.
6.
7. int mul(int a,int b)
8. {
9. return a*b;
10. }
11. float mul(double x, int y)
12. {
13. return x*y;
14. }
15. int main()
16. {
17. int r1 = mul(6,7);
18. float r2 = mul(0.2,3);
19. std::cout << "r1 is : " <<r1<< std::endl;
20. std::cout <<"r2 is : " <<r2<< std::endl;
21. return 0;
22. }
Output:
r1 is : 42
r2 is : 0.6
When the compiler shows the ambiguity error, the compiler does not run
the program.
o Type Conversion.
o Function with default arguments.
o Function with pass by reference.
o Type Conversion:
1. #include<iostream>
2. using namespace std;
3. void fun(int);
4. void fun(float);
5. void fun(int i)
6. {
7. std::cout << "Value of i is : " <<i<< std::endl;
8. }
9. void fun(float j)
10. {
11. std::cout << "Value of j is : " <<j<< std::endl;
12. }
13. int main()
14. {
15. fun(12);
16. fun(1.2);
17. return 0;
18. }
1. #include<iostream>
2. using namespace std;
3. void fun(int);
4. void fun(int,int);
5. void fun(int i)
6. {
7. std::cout << "Value of i is : " <<i<< std::endl;
8. }
9. void fun(int a,int b=9)
10. {
11. std::cout << "Value of a is : " <<a<< std::endl;
12. std::cout << "Value of b is : " <<b<< std::endl;
13. }
14. int main()
15. {
16. fun(12);
17.
18. return 0;
19. }
1. #include <iostream>
2. using namespace std;
3. void fun(int);
4. void fun(int &);
5. int main()
6. {
7. int a=10;
8. fun(a); // error, which f()?
9. return 0;
10. }
11. void fun(int x)
12. {
13. std::cout << "Value of x is : " <<x<< std::endl;
14. }
15. void fun(int &b)
16. {
17. std::cout << "Value of b is : " <<b<< std::endl;
18. }
Where the return type is the type of value returned by the function.
1. #include <iostream>
2. using namespace std;
3. class Test
4. {
5. private:
6. int num;
7. public:
8. Test(): num(8){}
9. void operator ++() {
10. num = num+2;
11. }
12. void Print() {
13. cout<<"The Count is: "<<num;
14. }
15. };
16. int main()
17. {
18. Test tt;
19. ++tt; // calling of a function "void operator ++()"
20. tt.Print();
21. return 0;
22. }
Output:
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5.
6. int x;
7. public:
8. A(){}
9. A(int i)
10. {
11. x=i;
12. }
13. void operator+(A);
14. void display();
15. };
16.
17. void A :: operator+(A a)
18. {
19.
20. int m = x+a.x;
21. cout<<"The result of the addition of two objects is : "<<m;
22.
23. }
24. int main()
25. {
26. A a1(5);
27. A a2(4);
28. a1+a2;
29. return 0;
30. }
Output:
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat(){
6. cout<<"Eating...";
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void eat()
13. {
14. cout<<"Eating bread...";
15. }
16. };
17. int main(void) {
18. Dog d = Dog();
19. d.eat();
20. return 0;
21. }
Output:
Eating bread...
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. int x=5;
6. public:
7. void display()
8. {
9. std::cout << "Value of x is : " << x<<std::endl;
10. }
11. };
12. class B: public A
13. {
14. int y = 10;
15. public:
16. void display()
17. {
18. std::cout << "Value of y is : " <<y<< std::endl;
19. }
20. };
21. int main()
22. {
23. A *a;
24. B b;
25. a = &b;
26. a->display();
27. return 0;
28. }
Output:
Value of x is : 5
In the above example, * a is the base class pointer. The pointer can only
access the base class members but not the members of the derived class.
Although C++ permits the base pointer to point to any object derived
from the base class, it cannot directly access the members of the derived
class. Therefore, there is a need for virtual function which allows the base
pointer to access the members of the derived class.
1. #include <iostream>
2. {
3. public:
4. virtual void display()
5. {
6. cout << "Base class is invoked"<<endl;
7. }
8. };
9. class B:public A
10. {
11. public:
12. void display()
13. {
14. cout << "Derived Class is invoked"<<endl;
15. }
16. };
17. int main()
18. {
19. A* a; //pointer of base class
20. B b; //object of derived class
21. a = &b;
22. a->display(); //Late Binding occurs
23. }
Output:
1. #include <iostream>
2. using namespace std;
3. class Base
4. {
5. public:
6. virtual void show() = 0;
7. };
8. class Derived : public Base
9. {
10. public:
11. void show()
12. {
13. std::cout << "Derived class is derived from the base clas
s." << std::endl;
14. }
15. };
16. int main()
17. {
18. Base *bptr;
19. //Base b;
20. Derived d;
21. bptr = &d;
22. bptr->show();
23. return 0;
24. }
Output:
In the above example, the base class contains the pure virtual function.
Therefore, the base class is an abstract base class. We cannot create the
object of the base class.