Class Definitions When you define a class you

  • Slides: 13
Download presentation
Class Definitions When you define a class, you define a blueprint for a data

Class Definitions When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object. 1

Class definitions A class definition starts with the keyword class followed by the class

Class definitions A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. We can declare a class using the following syntax 2

Class definitions class_name { Access-modifier: Data and functions … }; 3

Class definitions class_name { Access-modifier: Data and functions … }; 3

The term class is keyword which indicate that a class is going to be

The term class is keyword which indicate that a class is going to be declared, class_name is the name of class given by the user (user-defined) and inside the curly braces is the body of the class which can be consist of data or function or maybe both of them. 4

Note: Access-modifier shows the accessibility of data which can be private, public and protected

Note: Access-modifier shows the accessibility of data which can be private, public and protected now we will assume it as public, later we will have a detail description about Access-modifiers. 5

Example program class Marks { public: int math, cpp, graphic; // data of the

Example program class Marks { public: int math, cpp, graphic; // data of the class void print. Marks()// function of the class { cout<<“the subjects are : n”; cout<<“math : “<<math<<endl; cout<<“c++ : “<<cpp<<endl; cout<<“graphic : “<<graphic<<endl; } }; 6

q q q All the Data and function declared inside the class is known

q q q All the Data and function declared inside the class is known as members of the class. Data declared inside a class is called Data Members of respective class. Function declared inside a class is called member function of respective class. 7

Hence in previous example math, cpp , graphic and print. Data() are members of

Hence in previous example math, cpp , graphic and print. Data() are members of class Marks where math, cpp and graphic are data members and print. Marks() is member function. 8

Objects Declaration A class provides the blueprints for objects, so basically an object is

Objects Declaration A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Marks: Marks s 1, s 2; Syntax: Class. Name object. Name 1, object. Name 2…. ; 9

Accessing Data members The public data members of objects of a class can be

Accessing Data members The public data members of objects of a class can be accessed using the member access operator (. ). Syntax: object. Name. data. Member; We will consider the following example: 10

#include<conio. h> #include<iostream. h> class Marks { public : int cpp, math, physic; };

#include<conio. h> #include<iostream. h> class Marks { public : int cpp, math, physic; }; void main() { Marks obj 1; 11

cout<<"Enter the Marks : n"; cin>>obj 1. cpp; cin>>obj 1. math; cin>>obj 1. physic;

cout<<"Enter the Marks : n"; cin>>obj 1. cpp; cin>>obj 1. math; cin>>obj 1. physic; cout<<"Marks are : n"; cout<<"Math : "<<obj 1. cpp<<endl; cout<<"Physic : "<<obj 1. math<<endl; cout<<"cpp : "<<obj 1. cpp<<endl; getch(); } 12

thanks 13

thanks 13