C++ Basics Presentation
C++ Basics Presentation
Keywor User-defined
d name
class ClassName{
Access Specifier ; //can be public , private or
protected
Data Members ; //Variables to be used
Member Functions() //Methods to access data
members
{}
}; // class name ends
Declaring
with aObjects
semicolon
ClassName ObjectName ;
#include <iostream>
using namespace std;
Simple Example
class Phone{
string p_name;
string p_size;
public:
Phone(string name , string size){
p_name = name;
p_size = size;
}
void makeCall (){
cout << "Making Call using "<<p_name <<endl;
}
void receiveCall(){
cout << "Receiving Call using "<<p_name <<endl;
}
};
int main(){
Phone iphone("I Phone X","2000x500");
iphone.makeCall();
iphone.receiveCall();
#include<iostream>
using namespace std;
class Rectangle{
Int ht , wd;
Public void set_val(int x , int y){ht = x ; wd = y;}
Int area (){return ht*wd;}
};
class Rocket{
Void fly() {……………….}
};
Int main(){
Rectangle obj ;
}
Data Hiding
:
What is it ?
It is an object oriented programming concept that hide data from user so that
accidental changes can be avoided .
#include<iostream>
#include<iostream> using namespace std;
using namespace std; class Rectangle{
class Rectangle{ public:
private: int ht , wd;
int ht , wd; public :
public : void set_val(int x , int y){ht = x ; wd = y;}
void set_val(int x , int y){ht = x ; wd = y;} int area (){return ht*wd;}
int area (){return ht*wd;} void setHeight (int height) {
void setHeight (int height) { if(height != 0 && height > 0) {ht =
if(height != 0 && height > 0) {ht = height;} height;}
} }
void setWidth (int width) { void setWidth (int width) {
if(height != 0 && height > 0) {wd = height;} if(height != 0 && height > 0) {wd =
} height;}
}
};
Int main(){ };
Rectangle obj ; Int main(){
obj. setHeight(3); Rectangle obj ;
obj. setWidth(2); obj. setHeight(3);
} obj. setWidth(2);