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

Class and Object1

1. The document discusses classes and objects in C++. It covers declaration of classes, class objects, access specifiers, initializing private and public data members, and classes with arrays as data members. 2. Examples are provided to demonstrate defining member functions inside and outside the class, initializing private data through public functions, and accessing private data through public member functions. 3. The document ends with an example of a class with an array of strings as a data member to store student names and marks, and exercises for students to practice.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Class and Object1

1. The document discusses classes and objects in C++. It covers declaration of classes, class objects, access specifiers, initializing private and public data members, and classes with arrays as data members. 2. Examples are provided to demonstrate defining member functions inside and outside the class, initializing private data through public functions, and accessing private data through public member functions. 3. The document ends with an example of a class with an array of strings as a data member to store student names and marks, and exercises for students to practice.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Classes and Objects

31/01/11

Outlines
Declaration of Class and Class Objects Access Specifiers Class with an array as data member

Declaration of Class
class identifiers { access Specifier : statements ; };

Example
class Cars { private : int carno; public : void Display() { cout<<carno<<endl;} void Insert() { cin>>carno;} };

Declaration of Objects
class Cars mycar; OR Cars mycar ;

Name of class

name of objects

Contd
Cars car1, car2, car3 ;

Name of class

Names of objects separated by coma

Example
#include<iostream> using namespace std; class Market { public: void display() { cout<<Welcome To This Market<<endl ;

Contd..
cout<<Here is the price list<<endl ; } }; int main() { Market L1 ; L1.display(); return 0 ; }

Defining a member function outside the class


For defining a function outside the class body one has to use the scope resolution operator(::) .
type class_name :: Function_name(type parameter1, ...,,,type parameter n) { statements ; }

Example
#include<iostream> using namespace std; class Market { public : void dicplay() ; };

Contd..
void Market :: display() { cout<<Welcome To This Market<<endl ; cout<<Here is the price list<<endl ; } int main() { Market L1 ; L1.display(); return 0 ; }

Initializing Private data members


An object of a class is constructed when its data members are initialized. If data is declared private, the initialization is carried by a public function which may be a constructor function.

Contd..
#include<iostream> using namespace std; class Market { private : int x, y, z ; public : void display1() { cout<< Welcome , Here is the price list <<endl;}

Contd..
void Setprice(int a, int b, int c) { x = a, y = b, z = c ; } void display2() { cout << Price of item1 = << x<<endl ; cout << Price of item2 = << y<<endl ; cout << Price of item1 = << z<<endl ; } }; int main() { Market L1 ;

Contd..
L1.Setprice(4,6,2) ; L1. display1(); L1.display2(); } Note : put the definition function outside the class: void Market:: Setprice(int a, int b, int c) { x = a, y = b, z = c ; }

Initializing public data


#include<iostream> using namespace std ; class Rect { public : int x, y ; int Area () { return x*y ; } };

Contd..
int main () { Rect R1 ; R1.x = 8; R1.y = 5; cout << Area = << R1.Area()<<endl; cout << Length = << R1.x<<endl ; cout<< Width = << R1.y<< endl; return 0 ; }

Accessing private data


Private data be accessible through public member function.

Example
#include<iostream> using namespace std ; class Rect { private : int x, y ; public : void setsides( int L, int W){ x = L, y = W ; } int Area (){ return x*y ;} void dimension() { cout << Length = <<x<<\tWidth = <<y<<endl; } };

Contd..
int main () { Rect R1; R1.setsides(8,5); cout<<Area = <<R1.Area()<<endl ; R1.Dimension() ; return 0 ; }

Class with an array as data member


#include<iostream> using namespace std; class List { private : int x[4]; public : void Display1() { cout<<Welcome To This Market<<endl ; cout<<Here is the price list<<endl ; }

Contd..
void Setprice (int x[4]) ; void display2() { for(int i = 0; i<4; i++) cout<< Price of item <<i+1<< = <<x*i]<<endl; }; void List ::Setprice(int a[4]) { for(int j= 0; j<4;j++) x[j] = a[j]; }

Contd..
void main() { List L1; int P[4] = {6,5,4,8} ; L1.Setprice(P); L1. Display1(); L1.Display2(); }

Class with an array of string as data member


#include<iostream> using namespace std ; class List { private : int x[4]; char Name[4][20]; public: void Display1() { cout<<Here is the Marks List<<endl;}

Contd..
void Setmarks(int x[4]) ; void Setnames(char Names[4][20]); void Display2() { for( int i = 0;i<4;i++) cout<<Marks obtained by <<Names*i+<< = <<x[i]<<endl; } };

Contd..
void List :: Setmarks(int a[4] ) { for( int j=0; j<4;j++) x[j] = a[j] ; } void List :: Setnames( char b[4][20]) { for( int m = 0 ; m<4; m++) { for( int p = 0 ; p<20 ; p++) Names [m][p] = b[m][p]; } }

Contd..
void main() { List L1; char Students [4][20]; cout<< Enter the names of 4 students:; for( int i = 0;i<4;i++) cin.getline(Students[i][20]); cout<<Enter Marks<<endl; int P[4]; for( int k = 0; k<4; k++) cin>>P[k]; { L1.Setmarks(P); L1.Setnames(Students); } L1.Display1() ; L1.Display2() ; }

Exercise
Wap to illustrate the preparation of marks list in 4 subjects and total marks. Wap in which you have to create a class Cubical with a method for initializing sides of cube, for calculating surface area, and volume. Through object of this class call them in main.

You might also like