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

Oop Lab 2

The document discusses object-oriented programming concepts like classes, objects, attributes, and methods. It provides examples of creating a basic class called MyClass with integer and string attributes, as well as creating objects from the class and accessing attributes. It also covers access specifiers, creating multiple objects from the same class, and provides a task list with examples of creating classes to model real-world entities.

Uploaded by

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

Oop Lab 2

The document discusses object-oriented programming concepts like classes, objects, attributes, and methods. It provides examples of creating a basic class called MyClass with integer and string attributes, as well as creating objects from the class and accessing attributes. It also covers access specifiers, creating multiple objects from the same class, and provides a task list with examples of creating classes to model real-world entities.

Uploaded by

waqas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Today’s Motivation

“Learning to write programs


stretches your mind, and helps
you think better, creates a way
of thinking about things that I
think is helpful in all domains.”
— Bill Gates
LAB : Objects and Classes
Objective:
The objective of this lab is to practice the concepts of classes and
objects.

OOP
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions that
perform operations on the data, while object-oriented programming is
about creating objects that contain both data and functions.
Object-oriented programming has several advantages over procedural
programming:
 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with
less code and shorter development time
Classes:
A collection of objects with same properties and functions is known
as class. A class is used to define characteristics of the objects.
Syntax:
class identifier
{
body of the class
}
Example
Create a class called "MyClass":
class MyClass
{       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};

Example explained
 The class keyword is used to create a class called MyClass.
 The public keyword is an access specifier, which specifies that members (attributes and
methods) of the class are accessible from outside the class. You will learn more
about access specifiers later.
 Inside the class, there is an integer variable myNum and a string variable myString.
When variables are declared within a class, they are called attributes.
 At last, end the class definition with a semicolon ;.

Access Specifiers:
The commands are used to specify the access level of class members
are known as access specifiers.
 “private” access specifier
The private access specifier is used to restrict the use of class
member within the class. Any member of the class declared with private
access specifier can only be accessed within the class. It cannot be accessed
from outside the class. It is also the default access specifier.
 “public” access specifier
The public access specifier is used to allow the user to access a
class member within the class as well as outside the class. Any member of
the class declared with public access specifier can be accessed from
anywhere in the program.
Object:
When a class is defined, only the specification for the object is defined; no memory or storage is
allocated.

To use the data and access functions defined in the class, we need to create objects.

Create an object called "myObj" and access the attributes:

class MyClass {       // The class


  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};

int main() {
  MyClass myObj;  // Create an object of MyClass

  // Access attributes and set values


  myObj.myNum = 15; 
  myObj.myString = "Some text";

  // Print attribute values


  cout << myObj.myNum << "\n";
  cout << myObj.myString;
  return 0;
}

Multiple Objects
You can create multiple objects of one class:

Example
// Create a Car class with some attributes
class Car {
  public:
    string brand;   
    string model;
    int year;
};

int main() {
  // Create an object of Car
  Car carObj1;
  carObj1.brand = "BMW";
  carObj1.model = "X5";
  carObj1.year = 1999;

  // Create another object of Car


  Car carObj2;
  carObj2.brand = "Ford";
  carObj2.model = "Mustang";
  carObj2.year = 1969;

  // Print attribute values


  cout << carObj1.brand << " " << carObj1.model << " " <<
carObj1.year << "\n";
  cout << carObj2.brand << " " << carObj2.model << " " <<
carObj2.year << "\n";
  return 0;
}

LAB Task List


1. Write a program that declares a class with one integer data member and
two member functions in() and out() to input and output data in data
member.
#include<iostream>
using namespace std;
class Test
{
private:
int num;
public:
void in(){
cout<<”Enter a number: “;
cin>>num;
}
void out(){
cout<<”Number is “<<num;
}
};
int main()
{
Test obj;
obj.in();
obj.out();
return 0;
}
2. Write a class Marks with three data members to store three marks. Write
three member functions in() to input marks, sum() to calculate and return
the sum and avg() to calculate and return the average marks.
#include<iostream>
using namespace std;
class Marks
{
private:
int a, b, c;
public:
void in()
{
cout<<”Enter three marks: “;
cin>>a>>b>>c;
}
int sum()
{
return a+b+c;
}
float avg()
{
return (a+b+c)/3.0;
}
};
int main()
{
Marks m;
int s;
float a;
m.in();
s = m.sum();
a = m.avg();
cout<<”Sum = “<<s<<endl;
cout<<”Average = “<<a<<endl;
return 0;
}
C++ on the topic of classes:

1. Create a class called "Car" with a private member variable for its brand name. Add a public
member function to set the brand name, and another public member function to display the
brand name on the screen. Then, create an object of the "Car" class and use the set function to
set the brand name. Finally, use the display function to show the brand name on the screen.

2. Create a class called "Circle" with a private member variable for its radius. Add public member
functions to set the radius and to calculate the area of the circle. Then, create an object of the
"Circle" class and use the set function to set the radius. Finally, use the area function to display
the area of the circle on the screen.

3. Create a class called "Animal" with private member variables for its name and type. Add public
member functions to set the name and type, and another public member function to display the
animal's name and type on the screen. Then, create an object of the "Animal" class and use the
set functions to set the name and type. Finally, use the display function to show the animal's
name and type on the screen.

4. Create a class called "Rectangle" with private member variables for its length and width. Add
public member functions to set the length and width, and another public member function to
calculate the perimeter of the rectangle. Then, create an object of the "Rectangle" class and use
the set functions to set the length and width. Finally, use the perimeter function to display the
perimeter of the rectangle on the screen.

5. Create a class called "Fruit" with a private member variable for its name. Add a public member
function to set the name, and another public member function to display the name on the
screen. Then, create an array of objects of the "Fruit" class and use a loop to set the name of
each fruit in the array. Finally, use another loop to display the name of each fruit on the screen.

You might also like