Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Pointers

What is Pointers?

A Pointer points to a memory location.

Pointers used to access the information from a memory.

If memory is not there, and the data processing is not there (storing, accessing, processing the
data ).

Declare the pointers variable:

Normal variable Pointer Variable

Int a; Int* a; or int *a;

Types of pointers:

1. Typed Pointers
2. Untyped Pointers

Typed:

Points to specific type of data.

Ex: if we take integer pointer it points int data.

if we take double pointer it points doubledata.

Untyped:

Can points to any type of data, this is called as a generic pointer.

Void pointer.

In Pointers whatever the operation we do, we need to take the help of two Operators.

1. &(Address):

It will returns the address of a particular variable.memory location of a variable that what we
specified that it will be return.

2. *(Pointer):

The value which a specified address that will be return.


b (variable)
100(value)
2018(address)
Ex:

void main() m(pointer variable)


2018 (address value)
{

int b=100; 3014(address)


int* m;

m=&b;

printf("the b value is:%d\n",b);

printf("the &b value is:%u\n",&b);

printf("the m value is:%u\n",m);

printf("the &m value is:%u\n",&m);

printf("the *m value is:%d\n",*m);

printf("the *&b value is:%d\n",*&b);

o/p

the b value is:100                                                                                                             
the &b value is:1892334724                                                                                                     
the m value is:1892334724                                                                                                      
the &m value is:1892334728                                                                                                     
the *m value is:100                                                                                                            
the *&b value is:100  

call by value & call by reference

Normal Swapping(call by value)

#include <stdio.h>

void swap();
int main()

int a=10,b=5;

printf("before swap:");

printf("a=%d\nb=%d",a,b);

swap(a,b);

printf("a=%d\nb=%d",a,b); or printf("x=%d\ny=%d",x,y);

void swap(int x,int y)

int z;

z=x;

x=y;

y=z;

printf("\nafter swap:");

printf("a=%d\nb=%d",x,y);

Call by reference

#include <stdio.h>

void swap(); Main

A=10 B=5
int main()

{ A100 B1000
int a=10,b=5;

printf("before swap:");

printf("a=%d\nb=%d",a,b);

swap(&a,&b);

printf("a=%d\nb=%d",a,b);
X Swapp
y
}
A100 B1000
void swap(int* x,int* y)
X100 Y100
{

int z; Z 10

z=*x;

*x=*y;

*y=z;

printf("\nafter swap:");

printf("a=%d\nb=%d",*x,*y);

}
OOPS Concept Definitions

1. Objects
2. Classes
3. Abstraction
4. Encapsulation
5. Inheritance
6. Overloading

Class
Here we can take Human Being as a class. A class is a blueprint for any functional
entity which defines its properties and its functions. Like Human Being, having body
parts, and performing various actions.

Objects
My name is Abhishek, and I am an instance/object of class Male. When we say,
Human Being, Male or Female, we just mean a kind, you, your friend, me we are the
forms of these classes. We have a physical existence while a class is just a logical
definition. We are the objects.

Inheritance
Considering HumanBeing a class, which has properties like hands, legs, eyes etc,
and functions like walk, talk, eat, see etc. Male and Female are also classes, but
most of the properties and functions are included in HumanBeing, hence they can
inherit everything from class HumanBeing using the concept of Inheritance.

Abstraction
Abstraction means, showcasing only the required things to the outside world while
hiding the details. Continuing our example, Human Being's can talk, walk, hear, eat,
but the details are hidden from the outside world. We can take our skin as the
Abstraction factor in our case, hiding the inside mechanism.

Encapsulation
This concept is a little tricky to explain with our example. Our Legs are binded to help
us walk. Our hands, help us hold things. This binding of the properties to functions is
called Encapsulation.
Polymorphism
Polymorphism is a concept, which allows us to redefine the way something works, by
either changing how it is done or by changing the parts using which it is done. Both
the ways have different terms for them.
If we walk using our hands, and not legs, here we will change the parts used to
perform something. Hence this is called Overloading.
And if there is a defined way of walking, but I wish to walk differently, but using my
legs, like everyone else. Then I can walk like I want, this will be called
as Overriding.

Objects
Objects are the basic unit of OOP. They are instances of class, which have data
members and uses various member functions to perform tasks.

Class
It is similar to structures in C language. Class can also be defined as user defined
data type but it also contains functions in it. So, class is basically a blueprint for
object. It declare & defines what data variables the object will have and what
operations can be performed on the class's object.

Abstraction
Abstraction refers to showing only the essential features of the application and hiding
the details. In C++, classes can provide methods to the outside world to access &
use the data variables, keeping the variables hidden from direct access, or classes
can even declare everything accessible to everyone, or maybe just to the classes
inheriting it. This can be done using access specifiers.

Encapsulation
It can also be said data binding. Encapsulation is all about binding the data variables
and functions together in class.

Inheritance
Inheritance is a way to reuse once written code again and again. The class which is
inherited is called the Base class & the class which inherits is called
the Derived class. They are also called parent and child class.
So when, a derived class inherits a base class, the derived class can use all the
functions which are defined in base class, hence making code reusable.

Polymorphism
It is a feature, which lets us create functions with same name but different
arguments, which will perform different actions. That means, functions with same
name, but functioning in different ways. Or, it also allows us to redefine a function to
provide it with a completely new definition. You will learn how to do this in details
soon in coming lessons.

Syntax and Structure of C++ program

Sample prg:
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"welcome";

C++ Object and Class Example

#include<iostream.h>

class Student {
public:

int id; /data member (also instance variable)

char* name; //data member(also instance variable)

};

int main() {

Student s1; //creating an object of Student

s1.id = 201;

s1.name="KLN";

cout<<s1.id<<endl;

cout<<s1.name<<endl;

return 0;

C++ Class Example: Initialize and Display data through method

#include <iostream.h>

class Student {

public:

int id;//data member (also instance variable)

char* name;//data member(also instance variable)

void insert(int i, char* n)

id = i;

name = n;

}
void display()

cout<<id<<" "<<name<<endl;

};

int main(void) {

Student s1; //creating an object of Student

Student s2; //creating an object of Student

s1.insert(201, "Aarthi");

s2.insert(202, "Ramesh");

s1.display();

s2.display();

return 0;

C++ Class Example: Store and Display Employee Information


#include <iostream.h>

class Employee {

public:

int id;//data member (also instance variable)

char* name;//data member(also instance variable)

float salary;

void insert(int i, char* n, float s)

id = i;

name = n;
salary = s;

void display()

cout<<id<<" "<<name<<" "<<salary<<endl;

};

int main(void) {

Employee e1; //creating an object of Employee

Employee e2; //creating an object of Employee

e1.insert(201, "aarthi",990000);

e2.insert(202, "rakesh", 29000);

e1.display();

e2.display();

return 0;

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

C++ Default Constructor


#include <iostream.h>

class Employee

{
public:

Employee()

cout<<"Default Constructor Invoked"<<endl;

};

int main(void)

Employee e1; //creating an object of Employee

Employee e2;

return 0;

C++ Parameterized Constructor


#include <iostream.h>

class Employee {

public:

int id;//data member (also instance variable)

char* name;//data member(also instance variable)

float salary;

Employee(int i, char* n, float s)

id = i;

name = n;

salary = s;

void display()
{

cout<<id<<" "<<name<<" "<<salary<<endl;

};

int main(void) {

Employee e1 =Employee(101, "one", 890000); //creating an object of Employee

Employee e2=Employee(102, "two", 59000);

e1.display();

e2.display();

return 0;

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.

C++ Constructor and Destructor Example


#include <iostream.h>

class Employee

public:

Employee()

cout<<"Constructor Invoked"<<endl;

~Employee()

cout<<"Destructor Invoked"<<endl;
}

};

int main(void)

Employee e1; //creating an object of Employee

Employee e2; //creating an object of Employee

return 0;

C++ Inheritance

In C++, inheritance is a process in which one object acquires all the


properties and behaviors of its parent object automatically. In such way,
you can reuse, extend or modify the attributes and behaviors which are
defined in other class.

In C++, the class which inherits the members of another class is called
derived class and the class whose members are inherited is called base
class. The derived class is the specialized class for the base class.

C++ Single Level Inheritance Example: Inheriting Fields

#include <iostream.h>

// Base class

class Shape {

public:

void setWidth(int w) {

width = w;

void setHeight(int h) {

height = h;

}
protected:

int width;

int height;

};

// Derived class

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

};

int main(void) {

Rectangle Rect;

Rect.setWidth(5);

Rect.setHeight(7);

// Print the area of the object.

cout << "Total area: " << Rect.getArea() << endl;

return 0;

}
Multiple Inheritance
#include <iostream.h>

// Base class Shape

class Shape {

public:

void setWidth(int w) {

width = w;

void setHeight(int h) {

height = h;

protected:

int width;

int height;

};

// Base class PaintCost

class PaintCost {

public:

int getCost(int area) {

return area * 70;

};

// Derived class
class Rectangle: public Shape, public PaintCost {

public:

int getArea() {

return (width * height);

};

int main(void) {

Rectangle Rect;

int area;

Rect.setWidth(5);

Rect.setHeight(7);

area = Rect.getArea();

// Print the area of the object.

cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting

cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;

Multilevel

#include <iostream.h>
class Animal {

public:

void eat() {

cout<<"Eating..."<<endl;

};

class Dog: public Animal

public:

void bark(){

cout<<"Barking..."<<endl;

};

class BabyDog: public Dog

public:

void weep() {

cout<<"Weeping...";

};

int main(void) {

BabyDog d1;

d1.eat();

d1.bark();

d1.weep();

return 0;
}

Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"\nEnter the first number: ";
cin>>num1;
cout<<"\nEnter the second number: ";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"\nFor Subtraction:";
cout<<"\nEnter the first number: ";
cin>>n1;
cout<<"\nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"\nSum of "<<num1<<" and "<<num2<<"= "<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
void main()
{
clrscr();
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}

Method Overloading

#include <iostream.h>

class Cal {

public:

static int add(int a,int b){

return a + b;

static int add(int a, int b, int c)

return a + b + c;

};

int main(void) {

Cal C;

cout<<C.add(10, 20)<<endl;

cout<<C.add(12, 20, 23);

return 0;

Method overriding

#include <iostream.h>

class Animal {

public:

void eat(){

cout<<"Eating...";
}

};

class Dog: public Animal

public:

void eat()

cout<<"Eating bread...";

};

int main(void) {

Dog d ;

d.eat();

return 0;

Encapsulation

#include<iostream.h>

class Encapsulation

private:

// data hidden from outside world

int x;

public:

// function to set value of

// variable x
void set(int a)

x =a;

// function to return value of

// variable x

int get()

return x;

};

// main function

int main()

Encapsulation obj;

obj.set(5);

cout<<obj.get();

return 0;

Abstraction

#include <iostream.h>

class Sum

private: int x, y, z;

public:
void add()

cout<<"Enter two numbers: ";

cin>>x>>y;

z= x+y;

cout<<"Sum of two number is: "<<z<<endl;

};

int main()

Sum sm;

sm.add();

return 0;

You might also like