Вы находитесь на странице: 1из 49

ITE 6102

COMPUTER
PROGRAMMING 1
VIRTUAL CLASS – SEPTEMBER 11, 2020

SPEAKER: MS. KARREN V. DE LARA


OLC COMPUTING
WARM-UP AND REVIEW

• C++ Arrays
• C++ Functions
• C++ Functions Parameters
• C++ Functions Overloading
C++ OOP

• 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
C++ CLASSES AND OBJECTS

• A class is a template for objects, and an object is an instance of a class.


• When the individual objects are created, they inherit all the variables and functions from
the class.
• Example:

CLASS OBJECTS CLASS OBJECTS


Fruit Apple Cars Toyota
Banana Ford
Mango Honda
Grapes Kia
C++ CLASSES AND 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.
CREATING A CLASS

To create a class, use the class keyword:

Example:
Create a class called "MyClass":

class MyClass { // The class


public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
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.

• To access the class attributes (myNum and myString), use the dot syntax (.) on the
object
EXAMPLE

#include <iostream>

#include <string>

using namespace std;

class MyClass { // The class

public: // Access specifier

int myNum; // Attribute (int variable)

string myString; // Attribute (string variable)

};
EXAMPLE (CONT.)

int main() {

MyClass myObj; // Create an object of MyClass

// Access attributes and set values

myObj.myNum = 15;

myObj.myString = "Some text";


OUTPUT:
// Print values

cout << myObj.myNum << "\n"; 15


Some text
cout << myObj.myString;

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:


1. Inside class definition
2. Outside class definition
EXAMPLE (INSIDE CLASS)

#include <iostream> int main() {

using namespace std; MyClass myObj; // Create an object of MyClass

class MyClass { // The class myObj.myMethod(); // Call the method

public: // Access specifier return 0;

void myMethod() { // Method/function }

cout << "Hello World!";

}
OUTPUT:
};
Hello World!
EXAMPLE (OUTSIDE CLASS)

#include <iostream> int main() {

using namespace std; MyClass myObj; // Create an object of MyClass

class MyClass { // The class myObj.myMethod(); // Call the method

public: // Access specifier return 0;

void myMethod(); // Method/function declaration }

};

// Method/function definition outside the class


OUTPUT:
void MyClass::myMethod() {
Hello World!
cout << "Hello World!";

}
C++ CONSTRUCTORS

• A constructor in C++ is a special method that is automatically called when an object of


a class is created.

• Constructor is a special member function of a class that initializes the object of the
class. Constructor name is same as class name and it doesn’t have a return type.

• To create a constructor, use the same name as the class, followed by parentheses ().
EXAMPLE 1

#include <iostream> int main () {

using namespace std; MyClass myObj; // Create an object of MyClass (this will call the constructor)

class MyClass { // The class return 0;

public: // Access specifier }

MyClass() { // Constructor

cout << "Hello World!";

} OUTPUT:
};
Hello World!
EXAMPLE 2

#include <iostream>

using namespace std;

class constructorDemo {

public:

int num;

char ch;

constructorDemo() {

num = 100; ch = 'A';

};
EXAMPLE 2 (CONT.)

int main() {

constructorDemo obj;

cout<<"num: "<<obj.num<<endl;

cout<<"ch: "<<obj.ch;

return 0;

}
OUTPUT:

num: 100
ch: A
CONSTRUCTOR PARAMETERS

• Constructors can also take parameters (just like regular functions), which can be useful
for setting initial values for attributes.
EXAMPLE

#include <iostream>

using namespace std;

class Car { // The class

public: // Access specifier

string brand; // Attribute

string model; // Attribute

int year; // Attribute

Car(string x, string y, int z) { // Constructor with parameters

brand = x;

model = y;

year = z;
EXAMPLE (CONT.)

}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);

// Print values OUTPUT:


cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n"; BMW X5 1999
Ford Mustang 1969
return 0;
}
C++ ACCESS SPECIFIERS

The public keyword is an access specifier. Access specifiers define how the members (attributes and
methods) of a class can be accessed. In the example given, the members are public - which means
that they can be accessed and modified from outside the code.
However, what if we want members to be private and hidden from the outside world?

In C++, there are three access specifiers:


1. public - members are accessible from outside the class

2. private - members cannot be accessed (or viewed) from outside the class
3. protected - members cannot be accessed from outside the class, however, they can be accessed
in inherited classes.
EXAMPLE

#include <iostream>

using namespace std;


class MyClass {

public: // Public access specifier

int x; // Public attribute


private: // Private access specifier
int y; // Private attribute
};
EXAMPLE (CONT.)

int main() {

MyClass myObj;
myObj.x = 25; // Allowed (x is public)

myObj.y = 50; // Not allowed (y is private)

return 0;
} OUTPUT:

In function 'int main()':


Line 8: error: 'int MyClass::y' is private
Line 14: error: within this context
C++ ENCAPSULATION

• The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from
users. To achieve this, you must declare class variables/attributes as private (cannot be
accessed from outside the class). If you want others to read or modify the value of a
private member, you can provide public get and set methods.

Access Private Members


• To access a private attribute, use public "get" and "set" methods:
EXAMPLE

#include <iostream>

using namespace std;

class Employee {

private:

int salary;

public:

void setSalary(int s) {

salary = s;

}
EXAMPLE (CONT.)

int getSalary() {

return salary;

};

int main() {

Employee myObj;

myObj.setSalary(50000); OUTPUT:

cout << myObj.getSalary(); 50000


return 0;

}
WHY ENCAPSULATION?

• It is considered good practice to declare your class attributes as private (as often as you
can). Encapsulation ensures better control of your data, because you (or others) can
change one part of the code without affecting other parts
• Increased security of data
C++ INHERITANCE

• In C++, it is possible to inherit attributes and methods from one class to another. We
group the "inheritance concept" into two categories:
• derived class (child) - the class that inherits from another class
• base class (parent) - the class being inherited from
• To inherit from a class, use the : symbol.
SYNTAX OF INHERITANCE

class parent_class
{
//Body of parent class
};
class child_class : access_modifier parent_class
{
//Body of child class
};
ADVANTAGES OF USING INHERITANCE IN C++
PROGRAMMING
• Code Reusability
• Readability
EXAMPLE

class Animal {
// eat() function
// sleep() function
};

class Dog : public Animal {


// bark() function
};
C++ POLYMORPHISM

• Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.

• Like we specified in the previous slides; Inheritance lets us inherit attributes and
methods from another class. Polymorphism uses those methods to perform different
tasks.This allows us to perform a single action in different ways.
TYPES OF C++ POLYMORPHISM

• Compile time Polymorphism – This is also known as static (or early) binding.

Example: Function overloading and Operator overloading

• Runtime Polymorphism – This is also known as dynamic (or late) binding.

Example: Function overriding


EXAMPLE 1

#include <iostream>

using namespace std;

class Polygon {

protected:

int width, height;

public:

void set_values (int a, int b)

{ width=a; height=b; }

};
EXAMPLE (CONT.)

class Rectangle: public Polygon {

public:

int area()

{ return width*height; }

};

class Triangle: public Polygon {

public:

int area()

{ return width*height/2; }

};
EXAMPLE (CONT.)

int main () {

Rectangle rect;

Triangle trgl;

Polygon * ppoly1 = &rect;

Polygon * ppoly2 = &trgl;

ppoly1->set_values (4,5);

ppoly2->set_values (4,5); OUTPUT:


cout << rect.area() << '\n';
20
cout << trgl.area() << '\n';
10
return 0;

}
EXAMPLE 2

#include <iostream>
using namespace std;

// Function with 2 int parameters


int sum(int num1, int num2) {
return num1 + num2;
}
EXAMPLE 2 (CONT.)

// Function with 2 double parameters

double sum(double num1, double num2) {


return num1 + num2;
}

// Function with 3 int parameters


int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
EXAMPLE 2 (CONT.)

int main() {

// Call function with 2 int parameters

cout << "Sum 1 = " << sum(5, 6) << endl;

// Call function with 2 double parameters

cout << "Sum 2 = " << sum(5.5, 6.6) << endl;


OUTPUT:
// Call function with 3 int parameters

cout << "Sum 3 = " << sum(5, 6, 7) << endl; Sum 1 = 11


Sum 2 = 12.1
return 0; Sum 3 = 18
}
EXERCISE NO. 1

• Create a class called MyClass.


EXERCISE NO. 1 (ANSWER)

class MyClass
EXERCISE NO. 2

• Create an object of MyClass called myObj.


EXERCISE NO. 2 (ANSWER)

MyClass myObj;
EXERCISE NO. 3

• Use an access specifier to make members of MyClass accessible from outside the class.

class MyClass {

:
int myNum;
};
EXERCISE NO. 3 (ANSWER)

class MyClass {

public:
int myNum;
};
EXERCISE NO. 4

• Create an object of MyClass called myObj, and use it to set the value of myNum to 15.
class MyClass {
public:
int myNum;
};
int main() {
;
.;
cout << myObj.myNum;
return 0;
}
EXERCISE NO. 4 (ANSWER)

class MyClass {

public:

int myNum;

};

int main() {

MyClass myObj;

myObj.myNum = 15;

cout << myObj.myNum;

return 0;

}
EXERCISE NO. 5

• Create a constructor of MyClass, and call it:


class MyClass {
public:
{
cout << "Hello World!";
}
};
int main() {
myObj;
return 0;
}
EXERCISE NO. 5 (ANSWER)

class MyClass {

public:

MyClass() {

cout << "Hello World!";

};

int main() {

MyClass myObj;

return 0;

}
Thank you for listening.

If you have any questions please send a message thru LMS chat box or email me
at kvdelara@amaes.edu.ph

Вам также может понравиться