This document discusses objects and classes in C++. It covers const objects and const member functions. Const objects can only call const member functions, which do not modify the object. The document also discusses arrays as class data members and arrays of objects. It provides examples of declaring arrays within classes and declaring arrays of user-defined class objects. The examples demonstrate initializing and accessing class array members and array objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
41 views15 pages
Lec 7
This document discusses objects and classes in C++. It covers const objects and const member functions. Const objects can only call const member functions, which do not modify the object. The document also discusses arrays as class data members and arrays of objects. It provides examples of declaring arrays within classes and declaring arrays of user-defined class objects. The examples demonstrate initializing and accessing class array members and array objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15
Objects & Classes
Lecture 7
Instructor: Maria Amjad
Const and classes • Const keyword is used to make any element of a program constant. Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed, as it would violate the const-ness of the object. This includes both changing member variables directly (if they are public), or calling member functions that set the value of member variables. Example #include<iostream> using namespace std; class Demo { int value; public: Demo(int v = 0) {value = v;} void showMessage() { cout<<"Hello World We are Tushar, " "Ramswarup, Nilesh and Subhash Inside" " showMessage() Function"<<endl; } void display()const { cout<<"Hello world I'm Rancho " "Baba Inside display() Function"<<endl; } }; Example int main() { //Constant object are initialised at the time of declaration using constructor const Demo d1; //d1.showMessage();Error occurred if uncomment. d1.display(); return(0); } Output Hello world I'm Rancho Baba Inside display() Function Const member function • A const member function is a member function that guarantees it will not modify the object or call any non-const member functions (as they may modify the object). • Example class Something { public: int m_value;
Something(): m_value(0) { }
void resetValue() { m_value = 0; }
void setValue(int value) { m_value = value; }
int getValue() const; // note addition of const keyword here
};
int Something::getValue() const // and here
{ return m_value; } Const objects • A const object is defined the same for a user-defined type as a built-in type. • Example class X { int i; public: X(int ii); int f() const; }; X::X(int ii) : i(ii) {} int X::f() const { return i; } int main() { X x1(10); const X x2(20); x1.f(); x2.f(); } ///:~ Arrays Lecture 7 Array Fundamentals • C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. Example #include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; } Array as class members • Arrays can be declared as the members of a class. The arrays can be declared as private, public or protected members of the class. • Example #include<iostream> using namespace std; const int size=5; class student { int roll_no; int marks[size]; public: void getdata (); void tot_marks (); }; Array as class members void student :: getdata () { cout<<"\nEnter roll no: "; cin>>roll_no; for(int i=0; i<size; i++) { cout<<"Enter marks in subject"<<(i+1)<<": "; cin>>marks[i] ; } void student :: tot_marks() //calculating total marks { int total=0; for(int i=0; i<size; i++) total+ = marks[i]; cout<<"\n\nTotal marks "<<total; } int main() Student stu; stu.getdata() ; stu.tot_marks() ; return 0; } Array of objects • We created two objects rt1 and rt2 of class Rectangle representing the two rectangles. Rectangle rt1 ( 7, 4 ); - created the object 'rt1' and assigned its length and breadth as 7 and 4 respectively. Similarly, Rectangle rt2 ( 4, 5 ); created the object 'rt2' and assigned its length and breadth as 4 and 5 respectively. Example #include <iostream> #include <string> using namespace std; class Student { string name; int marks; public: void getName() { getline( cin, name ); } void getMarks() { cin >> marks; } Example void displayInfo() { cout << "Name : " << name << endl; cout << "Marks : " << marks << endl; } }; int main() { Student st[5]; for( int i=0; i<5; i++ ) { cout << "Student " << i + 1 << endl; cout << "Enter name" << endl; st[i].getName(); cout << "Enter marks" << endl; st[i].getMarks(); } for( int i=0; i<5; i++ ) { cout << "Student " << i + 1 << endl; st[i].displayInfo(); } return 0; } Output Student 1 Enter name Jack Enter marks Enter marks 87 54 Student 2 Student 1 Enter name Name : Jack Marx Marks : 54 Enter marks Student 2 45 Name : Marx Student 3 Marks : 45 Enter name Student 3 Julie Name : Julie Enter marks Marks : 47 47 Student 4 Student 4 Name : Peter Enter name Marks : 23 Peter Student 5 Enter marks Name : Donald 23 Marks : 87 Student 5 Enter name Donald