Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
118 views5 pages

Oop Lab 8

Pointer variables store the address of other variables. Pointers allow accessing and modifying the value of the variable being pointed to. Inheritance allows creating new classes from existing classes, where the new classes inherit properties and behaviors from the existing classes. Program 1 demonstrates defining a pointer variable, assigning the address of a variable to it, and accessing the value using the pointer. Program 2 shows a base Vehicle class and derived Car class, where creating a Car object invokes the Vehicle constructor.

Uploaded by

Khalid Akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views5 pages

Oop Lab 8

Pointer variables store the address of other variables. Pointers allow accessing and modifying the value of the variable being pointed to. Inheritance allows creating new classes from existing classes, where the new classes inherit properties and behaviors from the existing classes. Program 1 demonstrates defining a pointer variable, assigning the address of a variable to it, and accessing the value using the pointer. Program 2 shows a base Vehicle class and derived Car class, where creating a Car object invokes the Vehicle constructor.

Uploaded by

Khalid Akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming

Lab#8

Objective: To illustrate the concept of Pointer and Inheritance with hand-on practice.
Pointers
A pointer is a variable whose value is the address of another variable. Like any variable or
constant, you must declare a pointer before you can work with it. The general form of a pointer
variable declaration is −
type *var-name;
Following are the valid pointer declaration −
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
Using Pointers in C++
There are few important operations, which we will do with the pointers very frequently. (a) We
define a pointer variable. (b) Assign the address of a variable to a pointer. (c) Finally access the
value at the address available in the pointer variable. This is done by using unary operator * that
returns the value of the variable located at the address specified by its operand.
Inheritance
Inheritance in Object Oriented Programming can be described as a process of creating new
classes from existing classes.
New classes inherit some of the properties and behavior of the existing classes. An existing class
that is "parent" of a new class is called a base class. New class that inherits properties of the base
class is called a derived class.
Inheritance is a technique of code reuse. It also provides possibility to extend existing classes by
creating derived classes.
Inheritance Syntax
The basic syntax of inheritance is:
class DerivedClass : accessSpecifier BaseClass
Tasks:
• Write code, compile and run.
• Write output of code in below given box.
• Write another code (program) that demonstrates concept of this operator.
• Write the output of 2nd program.
Program 1 (Code)

#include <iostream>

using namespace std;

int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable

ip = &var; // store address of var in pointer variable

cout << "Value of var variable: ";


cout << var << endl;

// print the address stored in ip pointer variable


cout << "Address stored in ip variable: ";
cout << ip << endl;

// access the value at the address available in pointer


cout << "Value of *ip variable: ";
cout << *ip << endl;

return 0;
}

Output

Value of var variable: 20


Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20

Program 2 (Code)

#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}

Output:
This is a vehicle
Teacher’s Signature: _______________

You might also like