Last active
December 29, 2019 13:36
-
-
Save winhtut/22346456c4685d710beccdc8c3972710 to your computer and use it in GitHub Desktop.
Class and Object DeepDive C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class Dog{ | |
public : | |
string name; | |
int age; | |
float weight; | |
}; | |
int main(){ | |
Dog bobo; | |
Dog puppy; | |
bobo.age=5; | |
bobo.name="bobo"; | |
puppy.age=3; | |
bobo.weight=5.5; | |
puppy.weight=3.2; | |
cout<<"My Dog name is "<<bobo.name<<endl; | |
cout<<"Age of bobo"<<bobo.age<<endl; | |
cout<<"Age of puppy"<<puppy.age<<endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Book { | |
private: | |
int year; | |
double price; | |
public: | |
string title; | |
void printBook(); | |
void setPrice(double p); | |
void setYear(int y); | |
} book; | |
void Book::setPrice(double p) { | |
price = p; | |
} | |
void Book::setYear(int y) { | |
year = y; | |
} | |
void Book::printBook() { | |
cout << "Title: " << title << "\n"; | |
cout << "Price: " << price < "\n"; | |
cout << "Year: " << year << "\n"; | |
} | |
int main() { | |
book.title = "hooked"; | |
book.setYear(2013); | |
book.printBook(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Book { //declaration a class | |
private: | |
int year; | |
double price; | |
public: | |
string title; | |
void printBook(); | |
void setPrice(double p); | |
void setYear(int y); | |
}; | |
void Book::setPrice(double p) { | |
price = p; | |
} | |
void Book::setYear(int y) { | |
year = y; | |
} | |
void Book::printBook() { | |
cout << "Title of the book : " << title <<endl; | |
cout << "Price of the book : " << price <<endl; | |
cout << "Year : " << year << endl; | |
} | |
int main() { | |
Book book; | |
int bPrice=0; | |
cout<<"enter title of the book:"; | |
cin >> book.title; | |
cout<<"enter price of the book:"; | |
cin>>bPrice; | |
book.setPrice(bPrice); | |
book.setYear(2013); | |
book.printBook(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Parent { | |
protected: | |
int data; | |
}; | |
class Child : public Parent { | |
public: | |
void setData(int id) | |
{ | |
data = id; | |
} | |
void print() | |
{ | |
cout << "Protected data is: " | |
<< data << endl; | |
} | |
}; | |
int main() | |
{ | |
Child child; | |
child.setData(1001); | |
child.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment