Object Oriented Programming C++ Lecture No:03


  • Moving towards functionality
  • Class
    – Definition, syntax, example
  • Class, Class members, and objects
  • What you can do with objects
  • What you cannot do with objects
  • Working with multiple objects
  • Working with Visual C++ 6.0 Files

Moving Towards Functionality

  • We know that structures provides a way to group several variables of relevance into a single entity, e.g., Student
  • The next thing is: what can we do with the information of a student?
  • Moving towards functionalities relevant to a group of several variables.


  • A class is a programmer-defined data type. It consists of data structure and functions which operate on that data.
  • Placing data and functions together into a single entity is the central idea of object-oriented programming.

Class Declaration Syntax

class Name // usually capitalized
public members; // usually functions

private members; // usually variables

Example of a Class Declaration

using namespace std;
class ASimpleClass  //declare a class
	int data; //a private member
	void setData(int item)
		data = item;
	void displayData()
		cout<<"Data is : "<<data<<endl;
void main()
ASimpleClass asp; //creates an object 
asp.setData(10); //member access using dot operator

Data is : 10
Data is : 20
Press any key to continue


  • Preprocessor directive
    – Program Statements are instructions to the computer to do something.
    – Preprocessor directives are instructions to the compiler to do something.
    – A part of the compiler named preprocessor deals with these directives before the actual compilation starts.
    – The type of file included by #include is called a header file.
  • Using directive
    – C++ program can be divided into different namespaces.
    – A namespace is a part of the program where certain names are recognized.
    – Various program components like cout are declared within std.
    – We use using directive to avoid statements like std::cout<<“…”;

A few Terms

  • Class
  • Class member
    – Data Member
    – Member Function
  • Class object (class instance)


  • An object is an instance of a class.
  • Similarity:
    • int row;
    • This creates an instance of int called “row”.
  • Johan is an instance of the class Student.
  • CS1111 could be an array of 40 students and would be another instance of Student.
  • If the class is Student, the objects may be the individual students, the student courses, etc.

Calling Member Functions

  • asp.setData(10);
  • asp.displayData();
  • These two statements are not like normal function calls.
  • This strange syntax is used to call a member function that is associated with a specific object.
  • A member function must always be called in connection with an object of the class.
  • Because a member function is always called to act on a specific object, and not on the class in general.
  • The dot operator connects the object name and the member function.
  • Dot operator is also called class member access operator.

What you can do?

  • Create as many objects as you like
  • Work with individual members of an object


  • bilal.GPA + amin.GPA
  • bilal.GPA == amin.GPA
  • bilal.GPA = amin.GPA
  • bilal = amin //member-by-member assignment
  • amin.getGPA()

What you cannot do?

Working with Objects

Part Example

using namespace std;
class Part
 int modelno;
 int partno;
 float cost;
	void setPart(int mn, int pn, float c)
	void showPart()
	cout<<"Model : "<<modelno<<endl;
	cout<<"Part : "<<partno<<endl;
	cout<<"Cost : "<<cost<<endl;
void main()
Part part;

Model : 555
Part : 100
Cost : 100
Press any key to continue

Structures vs. Classes

We believe that

– Structures provide a way to group data.

Classes provide a way to group both data and functions.

Leave a Reply

Your email address will not be published. Required fields are marked *