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

C++ Classes and Objects-Jan

Uploaded by

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

C++ Classes and Objects-Jan

Uploaded by

Watiri Steve
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIVERSITY OF EMBU

DEPARTMENT OF MATHEMATICS, COMPUTING AND INFORMATION TECHNOLOGY

CSC 221: OBJECT ORIENTED ANALYSIS,DESIGN AND PROGRAMMING

WRITTEN BY:
ACSC 328: Object Oriented Copyright © UNIVERSITY OF EMBU,JANUARY2022
Dr. E.C TOO
Programming(C++)
1
All Rights Reserved
Classes and Objects
Class Definitions and Objects
Member Functions
Data Members
Get and Set functions
Constructors

INTRODUCTION TO CLASSES AND OBJECTS,


CONSTRUCTORS AND DESTRUCTORS
2
C++ Classes/Objects
C++ is an object-oriented programming language.
Everything in C++ is associated with classes and objects, along with its
attributes and methods.
 For example: in real life, a car is an object. The car has attributes, such
as weight and color, and methods, such as drive and brake.
Attributes and methods are basically variables and functions that
belongs to the class. These are often referred to as "class members".
A class is a user-defined data type that we can use in our program,
and it works as an object constructor, or a "blueprint" for creating
objects.

INTRODUCTION TO CLASSES AND OBJECTS,


CONSTRUCTORS AND DESTRUCTORS
3
C++ Classes/Objects
C++ supports object-oriented (OO) style of programming which allows
you to divide complex problems into smaller sets by creating objects.
Object is simply a collection of data and functions that act on those
data.
Create a Class
Before you create an object in C++, you need to
define a class.
A class is a blueprint for the object.
How to define a class in C++?
A class is defined in C++ using keyword class followed by the name of
class.
The body of class is defined inside the curly brackets and terminated by
a semicolon at the end.
class className
{
// some data
// some functions
};
Create a Class
To create a class, use the class keyword: Example explained
Example The class keyword is used to create a
class called MyClass.
Create a class called "MyClass":
The public keyword is an access
specifier, which specifies that members
(attributes and methods) of the class are
class MyClass { // The class accessible from outside the class. You
will learn more about access specifiers
public: // Access specifier later.
int myNum; // Attribute (int variable) Inside the class, there is an integer
string myString; // Attribute (string variable) 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 ;.
Create an Object
In C++, an object is created from a class.
We have already created the class named MyClass, so now we can use
this to create objects.
To create an object of MyClass, specify the class name, followed by the
object name.
Syntax to Define Object in C++
className objectVariableName;
To access the class attributes (myNum and myString), use the dot syntax
(.) on the object:
Create an Object
Example
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;
}
C++ Class Methods

Methods are functions that belongs to the class.


There are two ways to define functions that belongs to a
class:
Inside class definition
Outside class definition
Note: You access methods just like you access attributes;
by creating an object of the class and by using the dot
syntax (.):
Inside class definition
Example
class MyClass { // The class
public: // Access specifier
void Display() { // Method/function defined
inside the class
cout << “WELCOME TO OOP PROGRAMMING!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.Display(); // Call the method
return 0;
}
Outside class definition
class MyClass { // The class
public: // Access specifier
void Display(); // Method/function declaration
};
// Method/function definition outside the class
void MyClass::Display() {
cout << " WELCOME TO OOP PROGRAMMING!";
}
int main() {
MyClass myObj; // Create an object of MyClass
myObj.Display(); // Call the method
return 0;
}
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed);
};
int Car::speed(int maxSpeed) {
return maxSpeed;
}
int main() {
Car myObj; // Create an object of Car
cout << myObj.speed(200); // Call the method with an
argument
return 0;
}
EXAMPLE
// classes example void Rectangle::set_values (int x, int y) {

width = x;
#include <iostream>
height = y;
using namespace std;
}
class Rectangle {
int main () {
int width, height; Rectangle rect;
public: rect.set_values (3,4);

void set_values (int,int); cout << "area: " << rect.area();

int area() {return width*height;} return 0;

}
};
EXERCISES
Write a program to print the area and perimeter of a triangle having
sides of 3, 4 and 5 units by creating a class named 'Triangle' with a
function to print the area and perimeter.
Write a C++ program that will implement a class named sphere. The
program should accept the radius and determine the volume. The
program should then output the volume. Use pie as 3.14.
volume=4/3πr3.
SOLUTION…
#include <iostream>
#include <string>
int main()
#include <cmath>
using namespace std;
{
class Triangle
{
Triangle t;
public:
void print_area(int s1, int s2, int s3)
{
double s = (s1+s2+s3)/2.0;
t.print_area(3,4,5);
cout << s << endl;
cout << "Perimeter is " << (s1+s2+s3) << endl;
return 0;
};
}
}

You might also like