C++ Classes and Objects

Introduction
A class is a type definition of an Object Oriented Programming object in the C++ language. It is a derived type whose elements have other derived types. Elements of a class may have different data types or may be even functions..

The declaration begins with the key word class and ends with the required semicolon, i.e class scores{ //statements };

It is only a definition, and until an instance of this class (an actual object is created), it cannot then be used, (objects are derived from classes).

When we create an object from the class, we say we have instantiated the class.



A class has method and data, methods are also called behavior or services, data is also called properties or attributes.

Methods and data can be public or private members:

Public members

The method or data are accessible from outside the class. class students {  public: int marks(int c1, int c2, int ex);//member function 1 void address;//member function 2 void print;//member function 3 };

Private members

These are members or data that are only accessible from within the class. class students {  int cat1, cat2, exam; //data members };

Example 1: using namespace std; class marks { public: marks{cat1=0, cat2=0, cat3=0}//default constructor marks(int c1, int c2 int ex){cat1 = c1, cat2 = c2, exam = ex}//copy constructor ~marks{cout<<"Class destroyed \n";}//destructor int print; private: int cat1, cat2, exam; }; int main {  marks term1(70, 30, 80), term2(65, 56, 78);//objects cout<<"The score for term 1 is = ";term1.print; cout<<"The score for term 2 is = ";term2.print; return 0; } int marks::print { return (((cat1 + cat2)*30/200) + (exam*30/100)); } Example 2: class scores { public: int terms(int t1, int t2, int t3) {term1=t1; term2=t2;term3=t3;return (t1 + t2 + t3 / 300 * 100);} void grade(int trm1, int trm2, int trm3);//class member function ~terms{cout<<"Term ends"} //destructor ~average{cout<<"Average ends"}//destructor private: int term1, term2, term3;//data members }; int main { int cat1, cat2, exam; double score1, score2, score3; int tm1, tm2, tm3; cout<<"Enter marks for term 1 exams \n"; cout<<"--- \n"; cout<<"Enter marks for cat 1"<>cat1; cout<<"Enter marks for cat 1"<>cat2; cout<<"Enter marks for exam"<>exam; score1 = ((cat1 + cat2 + cat3)*30/300)+ (exam * 30/100) tm1 = int (score1); cout<>cat1; cout<<"Enter marks for cat 1"<>cat2; cout<<"Enter marks for exam"<>exam; score2 = ((cat1 + cat2 + cat3)*30/300)+ (exam * 30/100) tm2 = int (score2); cout<>cat1; cout<<"Enter marks for cat 1"<>cat2; cout<<"Enter marks for exam"<>exam; score3 = ((cat1 + cat2 + cat3)*30/300)+ (exam * 30/100) tm3 = int (score3); scores year1, year2, year3;//objects for class scores cout<<"The score for Year 1 is: ";year1.terms(tm1,tm2,tm3);cout<<"Grade: "<<year1.grade; cout<<"The score for Year 2 is: ";year2.terms(tm1,tm2,tm3);cout<<"Grade: "<<year2.grade; cout<<"The score for Year 3 is: ";year3.terms(tm1,tm2,tm3);cout<<"Grade: "<

Linked pages

 * 1) C++ Data Types
 * 2) C++ Variables & Constants
 * 3) C++ Operators & Operator Precedence
 * 4) C++ Preprocessor Directives
 * 5) C++ Input/Output
 * 6) C++ Control Structures
 * 7) C++ Jump Statements
 * 8) C++ Data Conversion and Formatting
 * 9) C++ Arrays
 * 10) C++ Pointers
 * 11) C++ Strings
 * 12) C++ Functions
 * 13) C++ Struct
 * 14) C++ Exceptions
 * 15) C++ Namespaces
 * 16) C++ Type Casting
 * 17) C++ Sample Codes
 * 18) Free C++ Projects