0% found this document useful (0 votes)
5 views3 pages

2)Classes and objects

The document provides an overview of Object-Oriented Programming (OOP) concepts in C++, including classes, objects, inheritance, polymorphism, encapsulation, and abstraction. It includes examples of class definitions, object creation, and methods for displaying student details and calculating total and percentage marks. Additionally, it outlines a task to create a student class with various attributes and methods to compute results.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

2)Classes and objects

The document provides an overview of Object-Oriented Programming (OOP) concepts in C++, including classes, objects, inheritance, polymorphism, encapsulation, and abstraction. It includes examples of class definitions, object creation, and methods for displaying student details and calculating total and percentage marks. Additionally, it outlines a task to create a student class with various attributes and methods to compute results.

Uploaded by

or12261618
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

------------- c++ oops ----------------

classes and objects


inheritance
polymorphism
encapsulation(data hiding)
abstraction

classes and objects


A class is a blueprint for the object.

We can think of a class as a sketch (prototype) of a house.


It contains all the details about the floors, doors, windows, etc.
Based on these descriptions we build the house. House is the object.

------------ Create a Class -------------


keyword -> class
class consist of variables and methods
methods can use of declared variable

class className {
// some data
// some functions
};

eg:

class Student
{
public:
int id; //field or data member

String name;//field or data member


};

to initialize the variable need objects:


Syntax to Define Object in C++

className objectVariableName;

eg:
Student s1;

C++ Access Data Members and Member Functions


eg:
s1.id=1;
s1.name="valar"

eg:
C++ Object and Class Example
#include <iostream.h>
class Student {
public:
int id;//data member (also instance variable)
char name[];//data member(also instance variable)
};
int main() {
Student s1; //creating an object of Student
s1.id = 201;
s1.name = "Sonoo Jaiswal";
cout<<s1.id<<"\n";
cout<<s1.name<<endl;
return 0;
}
----------- array of objects --------------

#include<iostream.h>
#include<conio.h>
class student{
public:
int id;
float tamil;

void display(){

cout<<"\nid="<<id;

cout<<"\nTamil="<<tamil;
}
};
void main(){

student s[2];
int i;
clrscr();
for(i=0;i<=1;i++){
cout<<"\n-----Enter student "<<(i+1)<<"Details --------" ;
cout<<"\nEnter the ID:";
cin>>s[i].id;
cout<<"\nEnter the mark:";
cin>>s[i].tamil;
}
for(i=0;i<=1;i++){
cout<<"\n---- student "<<(i+1)<<" details are ------";
s[i].display();
}
getch();
}
o/p:
-----Enter student 1 Details-------
Enetr the id:1
Enter the Mark:100
Enetr the id:2
Enter the Mark:100
-----student 1 Details are-------
id=1
mark=100
id=2
mark=100

---------- task -----------------


write a c++ program to create student class having int id,
tamil,english,maths,social,science,percentage,total.
also define the resukt function to calculate total and percentage of the student.

#include<iostream.h>
#include<conio.h>
class student{
public:
int id;
char name[100];
float tamil,english,maths,science,social,tot,percentage;

void display(){

cout<<"\nid="<<id;
cout<<"\nName="<<name;

cout<<"\nTamil="<<tamil;
cout<<"\nEnglish="<<english;
cout<<"\nMaths="<<maths;
cout<<"\nScience="<<science;
cout<<"\nSocial="<<social;
cout<<"\n Total ="<<tot;
cout<<"\nPercentage="<<percentage;
}
void result(){
tot=tamil+english+maths+science+social;
percentage=(tot/500)*100;
}
};
void main(){

student s[2];
int i;
clrscr();
for(i=0;i<=1;i++){
cout<<"\n-----Enter student "<<(i+1)<<"--------" ;
cout<<"\nEnter the ID:";
cin>>s[i].id;
cout<<"\nEnter the name: ";
cin>>s[i].name;
cout<<"\nEnter the Tamil mark:";
cin>>s[i].tamil;
cout<<"\nEnter the English mark:";
cin>>s[i].english;
cout<<"\nEnter the Maths mark:";
cin>>s[i].maths;
cout<<"\nEnter the Science mark:";
cin>>s[i].science;
cout<<"\nEnter the Social mark:";
cin>>s[i].social;
}
for(i=0;i<=1;i++){
cout<<"\n---- student "<<(i+1)<<" details are ------";
s[i].result();
s[i].display();
}
getch();
}

You might also like